簡體   English   中英

我如何 select 基於單元格值的列范圍?

[英]How do i select a range of columns based on cell values?

我正在嘗試 select 並根據工作簿 1 中單元格 C2 和單元格 C3 中的值復制工作簿 2 中的列范圍。

Sub copy_data1()

Dim wbmain_name, wb1_name As Variant
Dim wb1_ws1_name As Variant
Dim start_columnletter As String
Dim end_columnletter As String
Dim sheet_1 As Worksheet
Dim sheet_2 As Worksheet

wbmain_name = Range("C5")
wb1_name = Range("B9") & Range("B10")
wb1_ws1_name = Range("C10")

'Settings.
Set sheet_1 = Workbooks(wb1_name).Sheets(wb1_ws1_name) '‹ Specify here the source sheet.
Set sheet_2 = Workbooks(wbmain_name).Sheets(wb1_ws1_name) '‹ Specify here the destination sheet.
start_columnletter = Range("C2").Value
end_columnletter = Range("C3").Value


'Copying.
sheet_1.Range(start_columnletter & ":" & end_columnletter).Copy sheet_2.Range(start_columnletter & ":" & end_columnletter)

結束子

如果你想 select 一整列試試: Range("AS:AS").

解決您的問題非常簡單。 如果你想要列號:

Sub a()  
    
    Dim r As Range  
    Dim colNo As Integer  
    Set r = Range("AS:AS")  
    colNo = r.Column  
    MsgBox colNo  

End Sub

好久沒用 VBA,希望 go 不會出錯。

試試這個代碼:

Sub SubCopy()
    
    'Declarations.
    Dim start_columnletter As String
    Dim end_columnletter As String
    Dim sheet_1 As Worksheet
    Dim sheet_2 As Worksheet
    
    'Settings.
    Set sheet_1 = Workbooks("workbook 1.xlsm").Sheets(1) '‹ Specify here the source sheet.
    Set sheet_2 = Workbooks("workbook 2.xlsm").Sheets(1) '‹ Specify here the destination sheet.
    start_columnletter = sheet_1.Range("C2").Value
    end_columnletter = sheet_1.Range("C3").Value
    
    'Copying.
    sheet_1.Range(start_columnletter & ":" & end_columnletter).Copy sheet_2.Range(start_columnletter & ":" & end_columnletter)
    
End Sub



關於您的原始代碼:

    'get column letter
    start_columnletter = Range("C2").Value '‹ This will refer to the range C2 of the active sheet. It might not be the one you want.
    end_columnletter = Range("C3").Value '‹ This will refer to the range C2 of the active sheet. It might not be the one you want.
    
    'get column number based on column letter
    start_columnnumber = Range(start_columnletter & 1).Column '‹ This is not really necessary since you have start_columnletter already. Also it will refer to the active sheet.
    end_columnnumber = Range(end_columnletter & 1).Column '‹ This is not really necessary since you have end_columnletter already. Also it will refer to the active sheet.
    
    Workbook("workbook2").Range(Columns(start_columnnumber), Columns(end_columnnumber)).Select
    'The line up here won't work because:
    '-Workbook("workbook2") does not exist; Workbooks("workbook2") might be the one you are looking for;
    '-Workbook("workbook2").Range does not exist; Workbooks("workbook2").Sheets(1).Range might be the one you are looking for;
    '-the Select method will select the range; it will not copy it; avoid selecting objects as much as possible;
    '-the Select method will not select the range this way unless the parent sheet (and of course the parent workbook) is already selected; such might not be the case already.



“下標超出范圍”錯誤可能是由於工作簿名稱中的錯誤引用引起的。 確保它們被正確報告。 也許不應該有空格或文件擴展名不正確(.xls.xlsx.xlsm)。 關於您發布的最后一個代碼:

Sub copy_data1()
    
    Dim wbmain_name, wb1_name As Variant '‹ I'd suggest to set them as String.
    Dim wb1_ws1_name As Variant  '‹ I'd suggest to set it as String.
    Dim start_columnletter As String
    Dim end_columnletter As String
    Dim sheet_1 As Worksheet
    Dim sheet_2 As Worksheet
    
    wbmain_name = Range("C5")  '‹ I'd suggest to set is as Range("C5").Value ; what's in range C5? It also refers to the active sheet of the active workbook.
    wb1_name = Range("B9") & Range("B10")  '‹ Same as wbmain_name comment.
    wb1_ws1_name = Range("C10") '‹ Same as wbmain_name comment.
    
    'Settings.
    Set sheet_1 = Workbooks(wb1_name).Sheets(wb1_ws1_name)
    Set sheet_2 = Workbooks(wbmain_name).Sheets(wb1_ws1_name)
    start_columnletter = Range("C2").Value '‹ You should refer to sheet_1.
    end_columnletter = Range("C3").Value '‹ You should refer to sheet_1.
    
    
    'Copying.
    sheet_1.Range(start_columnletter & ":" & end_columnletter).Copy sheet_2.Range(start_columnletter & ":" & end_columnletter)
    
    
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM