我试图让我的代码提示用户选择3宽度和可变长度的数据范围。只有30个值,其中某些行为空白。我希望将这30个值填充到我的用户表单中的30个文本框中(这样就不必手动输入值)。我环顾四周,发现我的路线应该是Application.Inputbox
,然后将它传递给一个数组,如果空行可以用for循环除去。我不知道如何将用户选择的表传递到2D数组中。
Sub selectRange()
Dim r(1 To 14, 1 To 3) As Variant, ran As Range, calB(1 To 30) As Long, i As Integer, j As Integer, k As Integer, l As Integer
dozerCal.Hide
Set r = Application.InputBox("Select the Cal B table.", Type:=8)
For j = 1 To 14
For i = 1 To 3
If Abs(r(j, i)) > 0 Then
calB(l) = r(j, i)
l = l + 1
End If
Next
Next
lx = calB(1)
ly = calB(2)
lz = calB(3)
rx = calB(4)
ry = calB(5)
rz = calB(6)
ix = calB(7)
iy = calB(8)
iz = calB(9)
sx = calB(10)
sy = calB(11)
sz = calB(12)
p1x = calB(13)
p1y = calB(14)
p1z = calB(15)
p2x = calB(16)
p2y = calB(17)
p2z = calB(18)
lfx = calB(19)
lfy = calB(20)
lfz = calB(21)
lrx = calB(22)
lry = calB(23)
lrz = calB(24)
rfx = calB(25)
rfy = calB(26)
rfz = calB(27)
rrx = calB(28)
rry = calB(29)
rrz = calB(30)
ActiveWorkbook.Close
dozercall.Show
End Sub
提前感谢大家的帮助。
答案 0 :(得分:0)
编辑:我错过了您使用输入框错误,但我会留下这个答案,因为它提供了一种方法,可以将多维数组中的可变范围的用户输入折叠为单维数组。强>
这应该让你开始。基本上它将读取用户的输入,动态创建正确大小的一维数组(行*列),并读取用户选择的一维数组中的所有值。然后它将遍历一维数组并将值打印回窗口。
我认为这是您正在寻找的,但如果您需要进一步澄清,我可以添加一些。我添加了评论,因此您可以看到每个部分正在做什么。
Option Explicit
Private Sub TestArrays()
Dim calBTemp() As Variant, calB() As Variant
Dim i As Long, j As Long, x As Long
Dim rngInput As Range
Set rngInput = Application.InputBox("Select the Cal B table.", "Select Range", Type:=8)
'Read the user input, check for empty input
'If empty input, exit the subroutine
If Not rngInput Is Nothing Then
calBTemp = rngInput
Else
Exit Sub
End If
'Create the one-dimensional array dynamically based on user selection
ReDim calB((UBound(calBTemp, 1) - LBound(calBTemp, 1) + 1) * (UBound(calBTemp, 2) - LBound(calBTemp, 2) + 1))
'Loop through our multidimensional array
For i = LBound(calBTemp, 1) To UBound(calBTemp, 1)
For j = LBound(calBTemp, 2) To UBound(calBTemp, 2)
'Assign the value to our one dimensional array
calB(x) = calBTemp(i, j)
x = x + 1
Next j
Next i
'Loop through our one dimensional array
For i = LBound(calB) To UBound(calB)
Debug.Print calB(i)
Next i
End Sub
答案 1 :(得分:0)
所以我没有使用Application.Inputbox
权利。如果将其作为范围返回,它将配置为接缝的适当大小的2D阵列,您可以从那里调用/操作数据。这是一个工作分。
Sub selectRange()
Dim ran As Range, calB(1 To 30) As Double, i As Integer, j As Integer, k As Integer, l As Integer
dozerCal.Hide
Set ran = Application.InputBox("Select the Cal B table.", Type:=8)
l = 1
For j = 1 To 14
For i = 1 To 3
If Abs(ran(j, i)) > 0 Then
calB(l) = ran(j, i)
l = l + 1
End If
Next
Next
lx = calB(1)
ly = calB(2)
lz = calB(3)
rx = calB(4)
ry = calB(5)
rz = calB(6)
ix = calB(7)
iy = calB(8)
iz = calB(9)
sx = calB(10)
sy = calB(11)
sz = calB(12)
p1x = calB(13)
p1y = calB(14)
p1z = calB(15)
p2x = calB(16)
p2y = calB(17)
p2z = calB(18)
lfx = calB(19)
lfy = calB(20)
lfz = calB(21)
lrx = calB(22)
lry = calB(23)
lrz = calB(24)
rfx = calB(25)
rfy = calB(26)
rfz = calB(27)
rrx = calB(28)
rry = calB(29)
rrz = calB(30)
ActiveWorkbook.Close
dozerCal.Show
End Sub
答案 2 :(得分:0)
此代码将完成这一操作(并强制用户选择3列和14行):
import java.awt.Window;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import javax.swing.*;
public class DialogEg {
private static void createAndShowGui() {
// create JFrame for application
JFrame frame = new JFrame("Dialog Eg");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
@SuppressWarnings("serial")
class MainPanel extends JPanel {
private JTextField valueField = new JTextField(5);
public MainPanel() {
valueField.setFocusable(false); // so user can't interact with it
add(new JLabel("Value:"));
add(valueField);
add(new JButton(new GetValueAction("Get Value")));
}
private class GetValueAction extends AbstractAction {
private SecondPanel secondPanel = new SecondPanel();
public GetValueAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
// get JPanel's top level window
Window win = SwingUtilities.getWindowAncestor(MainPanel.this);
// create jdialog that is modal
JDialog dialog = new JDialog(win, "Get Value", ModalityType.APPLICATION_MODAL);
dialog.add(secondPanel);
// so the submit button will be activated when enter pressed:
dialog.getRootPane().setDefaultButton(secondPanel.getSubmitButton());
dialog.pack();
dialog.setLocationRelativeTo(win);
dialog.setVisible(true); // **** code flow stops here
// and resumes here once dialog is no longer visible
int value = secondPanel.getSpinnerValue();
valueField.setText("" + value);
}
}
}
@SuppressWarnings("serial")
class SecondPanel extends JPanel {
private SpinnerModel spinModel = new SpinnerNumberModel(-1, -1, 100, 1);
private JSpinner spinner = new JSpinner(spinModel);
private JButton submitButton = new JButton(new SubmitAction("Submit"));
public SecondPanel() {
add(spinner);
add(submitButton);
}
public int getSpinnerValue() {
return (Integer) spinner.getValue();
}
public JButton getSubmitButton() {
return submitButton;
}
private class SubmitAction extends AbstractAction {
public SubmitAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
putValue(MNEMONIC_KEY, mnemonic);
}
@Override
public void actionPerformed(ActionEvent e) {
int value = getSpinnerValue();
// get JPanel's top level window
Window win = SwingUtilities.getWindowAncestor(SecondPanel.this);
if (value < 0) {
String msg = "Submitted value must cannot be negative. Please try again";
JOptionPane.showMessageDialog(win, msg, "Invalid Entry", JOptionPane.ERROR_MESSAGE);
spinner.requestFocusInWindow(); // bring focus back to spinner
} else {
spinner.requestFocusInWindow();
win.dispose(); // get rid of dialog
}
}
}
}