[英]How do i select a range of columns based on cell values?
I'm trying to select and copy the range of columns in workbook 2 based on the values in cell C2 and cell C3 from workbook 1.我正在尝试 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)
End Sub结束子
If you want to select a whole column try: Range("AS:AS").如果你想 select 一整列试试: Range("AS:AS").
It's pretty simple to solve your problem.解决您的问题非常简单。 If you want the column number:
如果你想要列号:
Sub a()
Dim r As Range
Dim colNo As Integer
Set r = Range("AS:AS")
colNo = r.Column
MsgBox colNo
End Sub
Long time not working with VBA, hope it does not go wrong.好久没用 VBA,希望 go 不会出错。
Try this code:试试这个代码:
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
About your original code:关于您的原始代码:
'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.
The "subscript out of range" error might be caused by a wrong reference in the name of the workbooks. “下标超出范围”错误可能是由于工作簿名称中的错误引用引起的。 Make sure they are correctly reported.
确保它们被正确报告。 Maybe a space shouldn't be there or the file extension in not correct (.xls.xlsx.xlsm).
也许不应该有空格或文件扩展名不正确(.xls.xlsx.xlsm)。 About the last code you posted:
关于您发布的最后一个代码:
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.