I have a macro which checks for each cell of the first line of my 60 first columns, if the cell is empty, the macro automatically fills up the cell with the word “boubou” for every empty cell. However, I would like to fill up the empty cell not with the same word each time. For example, if the macro detects a first empty cell, I would like that it fills up with the word “boubou”, then for the second empty cell found, I would like that it fills up with the word “boubou2”, ”, then for the third empty cell found, I would like that it fills up with the word “boubou3” and so on…
Please find my VBA code below.
If someone knows the solutions, it would be fantastic.
Many thanks
Xavi
Sub hdfhgfdhhgf()
Dim i As Integer
For i = 1 To 60
If IsEmpty(Cells(1, i).Value) = True Then
Cells(1, i).Value = "boubou"
End If
Next i
End Sub
You already had the code, just needed to add & i
Option Explicit
Sub hdfhgfdhhgf()
Dim i As Integer
For i = 1 To 60
If IsEmpty(Cells(1, i)) Then Cells(1, i).Value = "boubou" & i
Next i
End Sub
Thought the first word will be "boubou1"
Related
I'm not very good at loops.
I'm trying to use VBA to loop through a column to look for any value, and then delete the entire row if it doesn't find anything (It's essentially a way of deleting rows of data that I've marked (or unmarked in this case)).
I've tried various things. My most recent attempt is below but its just deleting every row regardless of whether that cell has a value or not. Any suggestions?
Dim i as Long
For i = 1 To 50
If Cells(i, 1).Value = "" Then
Selection.EntireRow.Delete
Else
i = i + 1
End If
Next i
End Sub
There are several issues here:
When deleting rows in a loop work backwards, if going forward your row number changes as you delete.
There is no need to increment variable "i" next i already does this
Use the Worksheet object to delete the row rather than Selection
I would rewrite like this:
Sub delete()
Dim i As Long
For i = 50 To 1 Step -1
If Cells(i, 1).Value = "" Then
Worksheets("Sheet1").Rows(i).EntireRow.delete
End If
Next i
End Sub
I am trying to input a value into the next empty row in a column.
I tried using rows.count and off setting it like you would with a paste but it doesn't work. I was able to get it to change the value in the first cell but not to look for the next empty and change its value.
Private Sub CommandButton1_Click()
Dim wbCount As Workbook
Set wbEntry = ThisWorkbook
wbEntry.ActiveSheet.Range("B3").Copy
Set wbCount = Workbooks.Open("U:\MO# Count.xlsm")
Worksheets("Golf Cart").Paste _
Destination:=Worksheets("Golf Cart").Range("V5")
Worksheets("Golf Cart").Range("C2").Value = 1
wbEntry.Activate
Application.CutCopyMode = False
wbEntry.ActiveSheet.Range("O1").Select
End Sub
What I have works except for not searching for the next empty cell in "C" and changing the value to "1".
I'm confused, why would you think it searches for an empty cell?
Leaving the rest of the macro alone for now, this line:
Worksheets("Golf Cart").Range("C2").Value = 1
Does not search for an empty cell, it just writes a "1" into Cell C2. If you would like it to find the first empty cell in column C and then write a 1 into it, then this should do the trick:
Worksheets("Golf Cart").Cells(Sheets("Golf Cart").Cells(Rows.Count, 3).End(xlUp).Row + 1, 3) = 1
I'm not sure thats what you want tho, please clarify. This finds the last used row in column C and then goes to the next one down to write a "1" into it. It doesnt work if you want to find empty cells in an otherwise value-filled column. But you wrote something about rows.count or something, so I thought I'd post that way?
I have the following code that I've written to take some names and use them to populate a timesheet.
Sub InitNames()
Dim i As Integer
i = 0
Dim name As String
Windows("Employee Data.xlsx").Activate
Sheets("Employees").Select
Range("A2").Select
Do Until IsEmpty(ActiveCell)
name = ActiveCell.Value
Workbooks("Timesheet").Sheets("ST").Range("A9").Offset(i * 9).Value = name
ActiveCell.Offset(1, 0).Select
Loop
End Sub
Basically, the cells in the target sheet are spaced 9 rows away from each other, so the first name would go in cell A9, the second in A18, the third in A27, and so on. I'm sure I'm missing something incredibly simple, but I'm not getting any feedback from Excel whatsoever (no error messages). The cells in the timesheet are merged cells, but I cannot change them (locked by the owner), but I don't think that has anything to do with it.
EDIT: I added a line: OriginalValue = Workbooks("Timesheet").Sheets("ST").Range("A10").Offset((x - 2) * 9, 0).Value so I could watch to see what values were being overwritten in my Timesheet and I noticed something interesting: OriginalValue only grabs the first cell's text (A9), thereafter, for every cell (A18, A27, etc.) the debugger indicates that OriginalValue = "" even though those cells also contain names. However, when I open another worksheet and reference A9, A18, etc., I AM pulling the names.
EDIT 2: I modified the test line to read Workbooks("Timesheet").Sheets("ST").Range("A" & ((x - 1) * 9)).Value = "Test" which does change the values in all the target cells. Why would VBA allow me to assign "Test" to a cell value but not the names in the other worksheet?
Try something like this. It will accomplish the task you are requesting without using .Select or .Activate
Sub InitNames()
Dim i As Integer
Dim Wksht as Worksheet
i = 0
Set Wksht = Workbooks("Employee Data.xlsx").Sheets("Employees")
For i = 2 to Wksht.Range("A" & Wksht.Rows.Count).End(xlUp).Row
Workbooks("Timesheet").Sheets("ST").Range("A9").Offset((i-2) * 9,0).Value = Wksht.Range("A" & i).Value
Next i
End Sub
I've written a macro in VBA that simply fills in a given cell's value from another cell in that sheet. I do this for lots of cells in the sheet, and I'm doing it like so:
Range("B3").Value = Range("B200")
Range("B4").Value = Range("B201")
'etc.
Now, I am often adding values by inserting new rows, so I might insert a new row
between B200 and B201, which will break the macro because it doesn't autoupdate when
I insert the new row.
How can I code the macro so it autoupdates the cell references when I insert new rows or columns?
My suggestion would be to make sure the ROW you want to retrieve values from has a unique value in it that you can .FIND anytime you want, then grab your values from column B of that found cell's row. So right now you want to get a value in B200 and A200 always has the text in it: "Final Total" and that is unique.
Dim MyRNG As Range
Set MyRNG = Range("A:A").Find("Final Total", LookIn:=xlValues, LookAt:=xlWhole)
Range("B3").Value = Range("B" & MyRNG.Row)
Range("B4").Value = Range("B" & MyRNG.Row + 1)
This is not an answer but an alternative.
Naming your range is the way to go as Shiin suggested but then if you have 500 cells then like I mentioned earlier, naming 500 cells and using them in your code can be very painful. The alternative is to use smart code. Let's take an example
Let's say you have a code like this
Sub Sample()
Range("B3").Value = Range("B200")
Range("B4").Value = Range("B201")
Range("B5").Value = Range("B201")
' And
' So On
' till
Range("B500").Value = Range("B697")
End Sub
The best way to write this code is like this
Sub Sample()
Dim i As Long
For i = 200 To 697
Range("B" & i - 197).Value = Range("B" & i)
Next i
End Sub
and say if you insert a line at say row 300 then simply break the above code in two parts
Sub Sample()
Dim i As Long
For i = 200 To 299
Range("B" & i - 197).Value = Range("B" & i)
Next i
For i = 301 To 698
Range("B" & i - 197).Value = Range("B" & i)
Next i
End Sub
So every time you insert a row, simply break the for loop into an extra part. This looks tedious but is much better than naming 500 cells and using them in your code.
If you are planning to use the macro only once (i.e for 1 time use) then read ahead.
If you are worried that when the user inserts the row then the cells are not updated then you can instead of assigning a value, assign a formula.
For example
Range("B3").Formula = "=B200"
This will put a formula =B200 in cell B3. So next time when you insert a row so that the 200th row moves it's position, you will notice that the formula automatically gets updated in cell B3
HTH
Try giving a name to the range. If you refer to the range by name Excel searches for it and retrieves the rows that defines it. Range names update their definition when new rows are added.
Adding to the above, i think this tutorial illustrates my point:
http://www.homeandlearn.co.uk/excel2007/excel2007s7p6.html this is how to define the name of the range.
This tutorial explains how to use it on macros and vba:
http://excel.tips.net/T003106_Using_Named_Ranges_in_a_Macro.html
I hope this helps :D
I'm using VBA to do some further formatting to a generated CSV file that's always in the same format. I have a problem with my For Each Loop. the loop deletes an entire row if there is more than one blank row which can be determined from the first column alone.
Dim rowCount As Integer
For Each cell In Columns("A").Cells
rowCount = rowCount + 1
'
' Delete blank row
'
If cell = "" And cell.Offset(1, 0) = "" Then
Rows(rowCount + 1).EntireRow.Delete
spaceCount = 0
End If
Next
At some point the value in the loop one of the calls does not have a value of "", it's just empty and causes it to crash. To solve this I think that changing the type of the cell to text before that compare would work but I can't figure out how (no intellisense!!!)
So how do you convert a cell type in VBA or how else would I solve the problem?
Thanks.
Use cell.Value instead of the cell.Text as it will evaluate the value of the cell regardless of the formating. Press F1 over .Value and .Text to read more about both.
Be carefull with the statement For Each cell In Columns("A").Cells as you will test every row in the sheet (over a million in Excel 2010) and it could make Excel to crash.
Edit:
Consider also the funcion TRIM. It removes every empty space before and after a string. If in the cell there is a white space " "; it will look like empty for the human eye, but it has a space inside therefore is different than "". If you want to treat it like an empty cell, then try:
If Trim(cell.value) = "" then
As #andy (https://stackoverflow.com/users/1248931/andy-holaday) said in a comment, For Each is definitely the way to go. This even allows for there to be spaces in between lines.
Example code:
Sub ListFirstCol()
Worksheets("Sheet1").Activate
Range("A1").Activate
For Each cell In Application.Intersect(Range("A:A"), Worksheets("Sheet1").UsedRange)
MsgBox (cell)
Next
End Sub
Thanks Andy!