我有这段代码但硬编码。我需要它在循环或其他东西而不是case语句中自动化,直到空列停止,但更多列将添加到表单中。因此,只有当列标题存在然后它停止时,范围才能从ex int main()
{
string file_name;
int currentPos;
cout<<"Enter the file name";
getline(cin,file_name);
fstream f2(file_name, ios::ate | ios::out | ios::in);
if (!f2) //0 means error. You can also use f1.fail()
{
// open failed
cerr << "cannot open the file\n";
}
else
{
currentPos = f2.tellp();
cout << "Initial Write Pointer Position = " << currentPos << endl;
// Write some data. It should be written at the end.
f2 << 20;
currentPos = f2.tellp();
cout << "Write Pointer Position after write = " << currentPos << endl;
// Seek to specific position. File pointer must change accordingly.
f2.seekp(10);
currentPos = f2.tellp();
cout << "Write Pointer Position after seek = " << currentPos << endl;
// Write some data
f2 << "ABC";
currentPos = f2.tellp();
cout << "Final Write Pointer seek and write = " << currentPos << endl;
f2.close();
}
return 0;
}
中的B2
转到晚期列。它将显示每列中的内容。请注意,excel表名为Area
可以这样做吗?
Private Sub ComboBox3_Change()
Dim i As Long
i = ComboBox3.ListIndex
ComboBox4.Clear
Select Case i
Case Is = 0
With Worksheets("Area")
ComboBox4.List = .Range("**B2:B**" & .Range("**b**" & Rows.Count).End(xlUp).Row).Value
End With
Case Is = 1
With Worksheets("Area")
ComboBox4.List = .Range("**C2:C**" & .Range("**c**" & Rows.Count).End(xlUp).Row).Value
End With
Case Is = 2
With Worksheets("Area")
ComboBox4.List = .Range("**D2:D**" & .Range("**d**" & Rows.Count).End(xlUp).Row).Value
End With
Case Is = 3
With Worksheets("Area")
ComboBox4.List = .Range("**E2:E**" & .Range("**e**" & Rows.Count).End(xlUp).Row).Value
End With
Case Is = 4
With Worksheets("Area")
ComboBox4.List = .Range("**F2:F**" & .Range("**f**" & Rows.Count).End(xlUp).Row).Value
End With
End Select
End Sub
答案 0 :(得分:2)
这样的事情对你有用:
Private Sub ComboBox3_Change()
Dim ws As Worksheet
Dim ColNum As Long
Set ws = ActiveWorkbook.Sheets("Area")
ColNum = Me.ComboBox3.ListIndex + 2
Me.ComboBox4.Clear
If ColNum < 2 Then Exit Sub 'Nothing selected
Me.ComboBox4.List = ws.Range(ws.Cells(2, ColNum), ws.Cells(ws.Rows.Count, ColNum).End(xlUp)).Value
End Sub
答案 1 :(得分:0)
如果要将列号枚举为列名,请使用Chr(65)
,其中65是A的ASCII值。
在您的情况下,将66添加到您的整数我喜欢Chr(66+i)
,返回B
此外,还有一项功能可以将列号转换为excel中的列名=SUBSTITUTE(ADDRESS(1,col_number,4),"1","")
,但我还没有在VBA中尝试过。
在您的具体情况下,您根本不需要案例陈述
Private Sub ComboBox3_Change()
Dim i As Long
i = ComboBox3.ListIndex
ComboBox4.Clear
With Worksheets("Area")
ComboBox4.List = .Range("**E2:E**" & .Range("**e**" & Rows.Count).End(xlUp).Row).Offset(0,i).Value
End Sub
可能需要一些调整,因为我无法测试它。但offset
0行和i
列是您的最佳解决方案