What I'm trying to do is take the values from 4 textboxes and put them into 4 different sheets, but they need to be Integers so the Average and Sum functions work. But if the value is Null to clear the textbox so that value isn't calculated into the Monthly Sums or Averages. I keep getting a Type Mismatch Error with the Else clause, and when I mouse over it, it shows me frmDataEntry.tbBloodDraws = "". I thought that the else clause would just take the value and change it to an Int as long as it wasn't empty or Null? What am I missing?
Private Sub btnOK_Click()
' Get currently selected Cell of the Data Entry Sheet
Dim DataEntryCell As String
DataEntryCell = ActiveCell.Address
'Copy values from the dialog box into the correct sheets
Worksheets("Blood Draws").Activate
Range(DataEntryCell).Select
If (IsNull(frmDataEntry.tbBloodDraws)) Then
ActiveCell.Value = ActiveCell.Clear
Else
ActiveCell.Value = CInt(frmDataEntry.tbBloodDraws)
End If
End Sub
Use ClearContents to remove only the content of the cell and then check with IsNumeric if the string can be interpreted as number.
If tbBloodDraws.Text = "" Then
ActiveCell.ClearContents
ElseIf IsNumeric(tbBloodDraws.Text) Then
ActiveCell.value = CInt(tbBloodDraws.Text)
End If
Related
I've got an Excel file with N rows and M columns. Usually data are organized one per row, but it can happens that a data occupy more than a row. In this case how can I express that the second (or next) row has to refer to the first row?
In this example, AP.01 has got 5 rows of description, so how can I say that the other 4 rows refer also to the first code?
EDIT once that I did the association I have to export my Excel file into an Access DB. So I want to see the tables with the correct data.
If I have only one row for the description I wrote this code and it works:
If grid(r, 3).Text.Length > 255 Then
code.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
Else
code.Description = grid(r, 3).Text.ToString
End If
Instead if I have more than one row for the description I wrote this code and it doesn't work:
Do While grid(r, 1).ToString = ""
If grid(r, 1).ToString = "" And grid(r, 3).ToString IsNot Nothing Then
Dim s As String
s = grid(r, 3).ToString
code.Description = grid((r - 1), 3).ToString & s
End If
Loop
If it is a one-off, try the below. This will basically put a formula in every cell that refers to the cell immediately above it:
Select column A (from top until bottom of list (row N)
Press ctrl + g to open the GoTo dialogue
Press Special
Select Blanks from the radio buttons
The above will select all the blank cells in column A. Now enter = and press up arrow. Enter the formula by holding down ctrl while pressing enter. That will enter the same formula in every cell.
Try
Sub Demo()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet3") 'change Sheet3 to your data sheet
With .Range("A:A").SpecialCells(xlCellTypeBlanks)
.FormulaR1C1 = "=R[-1]C"
.Value = .Value
End With
End Sub
From your question I Guess that, you must be define a variable for last column Value. and check the value in respective column, if it is empty then use column value if not empty then take current value as last value.
'Dim LastValue as string
LastValue = sheet("SheetName").cells(i,"Column Name").value
for i = 2 to LastRow '>>>> here i am assume you run code in for loop from row to
'to last count row(LastRow as variable)
'Put your sheet name at "SheetName" and column index (like "A","B","C"...) at "Column Name"
if sheet("SheetName").cells(i,"Column Name").value <>"" then
LastValue = sheet("SheetName").cells(i,"Column Name").value
end if
'(Do your stuff using LastValue , you may generate lastvalue 1, lastvalue2 ..etc)
next'for loop end here
I am still new to Visual Basic, and would like to create a macro that essentially fills in empty cells with a value (integer) of zero, while leaving existing cells in the range as they were (if not empty). My code currently is this:
Sub FillEmptyCellsWithZeros()
'this should fill empty cells with a 0 value for the range selected
Dim cell As Object
Dim y As Integer
y = 0
For Each cell In Selection
If y = Empty Then
Selection.Value = 0
ElseIf y <> Empty Then
Selection.Value = ActiveCell.Value
End If
Next cell
End Sub
I know that most likely my loop isn't doing anything in this piece of code, but I cannot seem to get the result and this code was the closest I got.
Any help would be appreciated.
Thank you
Not to take anything away from Scott's answer, but if you're interested in a non-looping answer, you can try something like:
Selection.SpecialCells(xlCellTypeBlanks).Value = 0
this may cause problems if you have a large selection with a lot of discontinuous ranges, but it should be pretty reliable.
Also, if you have a formula that returns a blank (e.g.):
=IF(E16="","")
it will not consider those as blank (meaning they will still "appear" blank after running the code), so your mileage may very.
You are over thinking a little:
Sub FillEmptyCellsWithZeros()
'this should fill empty cells with a 0 value for the range selected
Dim cell As Object
Dim y As Integer
y = 0
For Each cell In Selection
If cell = "" Then
cell = y
End If
Next cell
End Sub
I have a macro GETGUID(). Would want to call the macro on Column 'B' whenever the corresponding column 'A' has a value in it. If Column A is empty, then GETGUID macro should not be called for the corresponding column B.
I tried the following code, but it is not working. For a test, I am using range A1:A20 but it can be any range. GETGUID() macro works fine if I type manually in column B but I want this to work whenever corresponding cell in column A has some value.
My knowledge in using Macros is not so good. Here is the code I am trying:
Public Function GetGUID() As String
GetGUID = Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
End Function
Dim cell_to_test As Range, cells_changed As Range
Set cells_changed = Target("A1:A20")
Set cell_to_test = Range("B1:B20")
If Not Intersect(cells_changed, cell_to_test) Is Nothing Then
Range("B1:B12") = GetGUID()
End If
I've created a simple Macro which loops through each cell declared by Worksheets(1).Range("A1:A20"). If a value is present (not vbNullString) then the column to the right is populated with a GUID.
Private RANGE_CELL As Range
Public Function GetGUID() As String
GetGUID = Mid$(CreateObject("Scriptlet.TypeLib").GUID, 2, 36)
End Function
Public Sub Populate_B()
For Each RANGE_CELL In Worksheets(1).Range("A1:A20")
If RANGE_CELL <> vbNullString Then
RANGE_CELL.Offset(, 1).Value = GetGUID
Else
'// Do nothing.
End If
Next RANGE_CELL
End Sub
Let me know if you need anything, happy to help.
I have a user form that asks for a column reference to be entered into a TextBox. I am trying to make it so that the number replaces the letter in the code. I am using the Cells(a,b) format to do this. Which is why I need the number to replace the letter.
Worksheets("AIO").Cells(X, TextBox3).Value = Worksheets("FDSA").Cells(Y, TextBox4).Value
Hence in the previous code when: x=2, TextBox3.Value=A, y=4, and TextBox4.Value=AA
The code will work as
Worksheets("AIO").Cells(2, 1).Value = Worksheets("FDSA").Cells(4, 27).Value
The only thing I can think of making is a huge if statement were I code something similar like this:
If textbox3.value ="A" then
textbox3.value=1
elseif textbox3.value=E then
textbox3.value=5
.......
.......
textbox3.value=AD then
textbox3.value=30
End If
If you want to keep it uncomplicated, .Cells can use the column string instead of number
Sub test()
'From sheet module
Debug.Print Me.Cells(1, 1).Address 'prints $A$1
Debug.Print Me.Cells(1, "A").Address 'also prints $A$1
End Sub
You might have to validate that it's a valid column ID but you probably need to do that even converting it to a number.
On way to get a column number from a character string:
Sub ColumnNumber()
Dim s As String
Dim L As Long
s = "AB"
L = Cells(1, s).Column
MsgBox L
End Sub
I an trying to extract data from sheet "Record" by matching an entered reference number in sheet "Form" with those numbers in column B of "Record." I was able to come up with the VB code below through command button click. However, it will only return a single value from sheet "Record" column i and coding for each will really be time consuming.
Private Sub CommandButton1_Click()
With Application.WorksheetFunction
Sheets("Form").Range("b:b") = _
.Index(Sheets("Record").Range("h:h"), .Match(Sheets("Form").Range("i13"), Sheets("Record").Range("b:b"), 0), 1)
End With
End Sub
I'm wondering if is it possible to copy values from sheet "Record" columns H-Q to sheet "Form" columns B-K if the reference number in cell I13 of sheet "Form" matches any value on column B of sheet "Record?" Because what i encounter most of the time is returning the entire row.
I would really appreciate any help. Thanks
It might be brute force, but I think the best way is to loop through the data like this:
'Find the last row of data
Public Function Get_Last_Row_Find(ByVal rngToCheck As Range) As Long
Dim rngLast As Range
Set rngLast = rngToCheck.Find(what:="*", searchorder:=xlByRows, SearchDirection:=xlPrevious)
If rngLast Is Nothing Then
Get_Last_Row_Find = rngToCheck.Row
Else
Get_Last_Row_Find = rngLast.Row
End If
If Get_Last_Row_Find <= 1 Then
Get_Last_Row_Find = 2
End If
End Function
Public Sub CommandButton1_Click
x = Get_Last_Row_Find(Sheets("Record").Range("B:B")
for i = 1 to x
if Sheets("Form").Range("I13").Value = Sheets("Record").Range("B:B").Offset(i-1,0).Value then 'match
Worksheets("Record").Range("H"&i&":Q"&i).Copy _
destination:=Worksheets("Form").Range("B"&i&":K"&i)
next i
Note the two methods of "offsetting": you can use the .Offset method or you can use a variable and concatenate it within the Range("") text.
Code not tested.