Excel VBA:如何返回给定单元格的列号

时间:2019-01-03 19:24:24

标签: excel vba excel-vba function

在Excel VBA中,我试图将一个Sub放在一起,以获取要从一个位置复制到另一位置的文件名,并使用其给定的单元格列号作为文件名的前缀,以便它们出现在新文件夹中顺序与Excel中的行顺序相同。 到目前为止,除了将列号前缀添加到文件名的那一部分之外,我一切工作都很好。目前在代码的那一部分中,我有cellname.Column,它不起作用。

我是VBA的新手,所以我尝试从Stack Overflow和其他站点复制并粘贴一些代码来完成这项工作,但是目前还没有解决办法。

Sub copyfiles()

    Dim xRg As Range, xCell As Range
    Dim xSFileDlg As FileDialog, xDFileDlg As FileDialog
    Dim xSPathStr As Variant, xDPathStr As Variant
    Dim xVal As String

    On Error Resume Next

    Set xRg = Application.InputBox("Please select the file names:", "KuTools For Excel", ActiveWindow.RangeSelection.Address, , , , , 8)
    If xRg Is Nothing Then Exit Sub

    Set xSFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xSFileDlg.Title = "Please select the original folder:"
    If xSFileDlg.Show <> -1 Then Exit Sub
    xSPathStr = xSFileDlg.SelectedItems.Item(1) & "\"

    Set xDFileDlg = Application.FileDialog(msoFileDialogFolderPicker)
    xDFileDlg.Title = "Please select the destination folder:"
    If xDFileDlg.Show <> -1 Then Exit Sub
    xDPathStr = xDFileDlg.SelectedItems.Item(1) & "\"

    For Each xCell In xRg
        xVal = xCell.Value + ".pdf"
        If TypeName(xVal) = "String" And xVal <> "" Then
            FileCopy xSPathStr & xVal, xDPathStr & cellname.Column & xVal
        End If
    Next

End Sub

任何帮助将不胜感激!

0 个答案:

没有答案