简体   繁体   中英

Excel Userform Range

I am trying to get a userform textbox to put the entered data into cells in a specific column based on the date in that column when I click a button to send the data.

In B2 I have the first day of the month, B3 is the 2nd and it continues. I want the userform to reference the column with todays date in it, then send the data entered in the textbox into the same column but offset the row down so many rows. I have multiple textboxes that I wanted entered at a time. So if todays date was in V2, Textbox1 would enter data into V4, Textbox2 would be V5, Textbox3 would be V6, once all data is entered I want to click a button to send the information to the SS.

This is what I currently have;

Private Sub cmdRun_Click()
If txtImpl.Text = "" Then
MsgBox ("Error, Impulsiveness can't be empty")
Else
Range("V4").Value = txtImpl.Text
End If
If txtOrga.Text = "" Then
MsgBox ("Error, Organization can't be empty")
Else
Range("V5").Value = txtOrga.Text
End If
If txtTime.Text = "" Then
MsgBox ("Error, Time Managment can't be empty")
Else
Range("V6").Value = txtTime.Text
End If
If txtFocu.Text = "" Then
MsgBox ("Error, Focus can't be empty")
Else
Range("V7").Value = txtFocu.Text
End If
If txtRest.Text = "" Then
MsgBox ("Error, Restlessness can't be empty")
Else
Range("V8").Value = txtRest.Text
End If
If txtFrus.Text = "" Then
MsgBox ("Error, Frustration can't be empty")
Else
Range("V9").Value = txtFrus.Text
End If
End Sub

This lets me enter text into my one textbox, then click the "run" button to send it to the desired cell "V4-V9" or if there's no data it puts back a message.
I can't figure out how to get the "Range("V4")" - "Range("V9") to reference a not specific cell but rather a cell based on conditions. I have tried to use Index(Match) but I can't get formulas to work correctly in VBA.
Buttons are cmdRun and cmdClos

I am pretty new to this and am probably not even doing this the most efficient way but I am open to suggestions

I am pretty sure this would be what you're looking for. It'd probably be best to have a counter by row, but you could continue your approach as hard coding. The function being called sets the column.

Private Sub cmdRun_Click()
Dim ws As Worksheet
   Set ws = ActiveSheet 'better than leaving blank, better if specified by ID.

Dim entryColumn As Long
   entryColumn = getTodayColumn(ws.Range("B2").EntireRow)


With ws.Cells(1, entryColumn).EntireColumn

If txtImpl.Text = "" Then
   MsgBox ("Error, Impulsiveness can't be empty")
Else
    .Cells(4, 1).Value = txtImpl.Text
End If


If txtOrga.Text = "" Then
   MsgBox ("Error, Organization can't be empty")
Else
   .Cells(5, 1).Value = txtOrga.Text
End If

'etc......

End With

End Sub


Private Function getTodayColumn(searchRange As Range) As Long
Dim aCell As Range

   For Each aCell In Intersect(searchRange.Worksheet.UsedRange, searchRange).Cells
      If aCell.Value = WorksheetFunction.RoundDown(Now, 0) Then
         getTodayColumn = aCell.Column
         Exit For
      End If
   Next aCell
   
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM