So my question is this I have this code here:
Private Sub Validate_Input_Click()
Dim temp As String
For Row = 2 To 250
temp = ""
For col = 2 To 12
If Cells(Row, col) <> "" Then
If temp <> "" Then temp = temp & "_"
temp = temp & Cells(Row, col)
End If
Next col
Cells(Row, 1) = temp
Next Row
End Sub
I have information from Column 2 all the way to 12 and works perfect. But now I'm trying to figure out if lets say, Rows 2-6 have all information from lines 2-6 inputted correctly all cells are filled in, Line 7 is missing a cell or two that isn't filled out and lines 8-12 inputted correctly, all cells are filled in, how do I make it so the macro Concatenates lines 2-6, skips 7 because of blank cells, and continues to concatenate 8-12?
Also, not all rows from 2 - 250 are filled out, usually goes to about 60 to 75 depending on what I'm doing with the worksheet at that time. Just want the extra buffer just in-case also why I'm trying to figure out a way to skip over blank cells and not concatenate that specific row or rows that have blank cells.
I've been messing around with If Else statements but can't quite get it.
Any help would be great!
We test for any blanks before processing the row:
Sub Validate_Input_Click()
Dim temp As String
For Row = 2 To 250
If Application.WorksheetFunction.CountBlank(Range(Cells(Row, 2), Cells(Row, 12))) = 0 Then
temp = ""
For col = 2 To 12
If Cells(Row, col) <> "" Then
If temp <> "" Then temp = temp & "_"
temp = temp & Cells(Row, col)
End If
Next col
Cells(Row, 1) = temp
End If
Next Row
End Sub
Related
thank you for the help in advance!
I am creating a macro that writes to a text file. I am having trouble referencing a range inside of a for loop. There are two examples I have where the range I have called returns a "". For the first image below, this is where the user inputs data using a combination of entering data into the sheet and using a button that directs them to a user form that plops in what they entered. In this first image, where the user is directed by the userform, the data is outputted to another sheet and I use vlookup to display the info. I do this to run operations on the other sheet and just to help me keep track of everything, (novice vba user). The second sheet is where data is stored and calculations are run.
1st image, User input and display 2nd image, data storage and calculation sheet
newDimName only outputs "_Cav" and the value stored in cavNum for the loop. The intention is concatenate the string inside of column A with more information depending on what the user has inputted. I suspect I am not referencing the range for column a correctly. The Loop is set to go to 2 to whatever amount of "dimensions" have been entered by the user (value in lastUserDim). The code below is contained with a sheet reference (not in the snippet) so it references the correct sheet. Ex. "With Sheet2", at before the loop and a "End With" at the end of the loop.
Dim a As Integer, cavNum As Integer
Select Case numCav
Case Is = 4
For a = 2 To lastUserDim
For cavNum = 1 To 4
newDimName = Cells(a, 1) & "_Cav" & cavNum
Next cavNum
Next a
totalColumns = 4 * lastUserDim
Case Is = 8
For a = 2 To lastUserDim
For cavNum = 1 To 8
newDimName = Cells(a, 1) & "_Cav" & cavNum
Next cavNum
Next a
totalColumns = 8 * lastUserDim
Case Is = 16
For a = 2 To lastUserDim
For cavNum = 1 To 16
newDimName = Cells(a, 1) & "_Cav" & cavNum
Next cavNum
Next a
totalColumns = 16 * lastUserDim
Case Is = 32
For a = 2 To lastUserDim
For cavNum = 1 To 32
newDimName = Cells(a, 1) & "_Cav" & cavNum
Next cavNum
Next a
totalColumns = 32 * lastUserDim
Case Else
MsgBox "Please select what # of cavities this tool has."
End Select
For the code below, I want the the range I defined to be read and for the select case to determine if a cell contains yes or no. The loop is intended to run this operation for number of user inputted data. Again I suspect I am not calling the data in column c correctly like I was previously with Column A. I tried using cells like in the first code snippet, just range, range("").value, and range("").text.
Dim LSLBound As String, LSLBoundRef As String
Dim c As Integer
For c = 2 To lastUserDim
LSLBoundRef = Range("C2:C" & c).Text 'ref to cell value to use for select case
Select Case LSLBoundRef
Case Is = "No"
LSLBound = "LBound 1;"
Case Is = "Yes"
'Do Nothing
End Select
Next c
If the second code snippet is wrapped in a "With Sheet2", you need to preface the Range with a period, ie **.**Range("C2")... so that it is using the correct sheet.
Dim LSLBound As String, LSLBoundRef As String
Dim c As Integer
With Sheets("Sheet2")
For c = 2 To lastUserDim
LSLBoundRef = .Range("C2:C" & c).Text 'ref to cell value to use for select case
Select Case LSLBoundRef
Case Is = "No"
LSLBound = "LBound 1;"
Case Is = "Yes"
'Do Nothing
End Select
Next c
End With
A good way to figure out what range is actually being referenced is by printing the range address to the immediate window with Debug.Print Range.Address.
I am concatenating every columns of the rows on excel sheet and I am already done with the concatenating of every columns.
My problem is I only need to concatenate the rows that does not contain letter T on column A starting row 3. please see image below
sample formula
=Sheet1!A3&Sheet1!B3&Sheet1!C3&Sheet1!D3&Sheet1!E3&Sheet1!F3&Sheet1!G3&Sheet1!H3&Sheet1!I3&Sheet1!J3&Sheet1!K3&Sheet1!L3&Sheet1!M3&Sheet1!N3&Sheet1
on that image, you can see the result below of the concatenated columns from the above details, but not all the time the rows to be concatenated there has the same number(like on the above image that has only 3 rows to be computed), do you know some code or formula for this matter?
You can try
=TEXTJOIN(A1:N1)
Or whatever your range should be.
But dynamically in VBA it done like this:
Dim row, col As Integer
Dim curStr As String
row = 1
col = 1
curStr = ""
Do While Sheets("Input").Cells(row, col) <> ""
Do While Sheets("Input").Cells(row, col) <> ""
curStr = curStr + Sheets("Input").Cells(row, col)
col = col + 1
Loop
col = 1
Sheets("Output").Cells(row, col) = curStr
row = row + 1
curStr = ""
Loop
Use this UDF - User Defined Function
Function conc(rangetoconc As Range) As String
Dim finalresult As String
Dim cell As Range
finalresult = vbNullString
If InStr(1, rangetoconc.Cells(1, 1), "T") = 0 Then
For Each cell In rangetoconc
If CStr(cell.Value) <> vbNullString And CStr(cell.Value) <> " " Then
finalresult = finalresult & CStr(cell.Value)
End If
Next
End If
conc = finalresult
End Function
I just figured out the answer to my question after reading some excel formula online.
=IF(A1="","",
IF(LEFT(A1,1)="T","",
IF('Sheet1'!A2<>"",'Sheet1'!A2&'Sheet1'!B2&'Sheet1'!C2&'Sheet1'!D2&'Sheet1'!E2&'Sheet1'!F2&'Sheet1'!G2&'Sheet1'!H2&'Sheet1'!I2&'Sheet1'!J2&'Sheet1'!K2&'Sheet1'!L2&'Sheet1'!M2&'Sheet1'!N2&'Sheet1'!O2&'Sheet1'!P2&'Sheet1'!Q2&'Sheet1'!R2&'Sheet1'!S2,"T "&MIN(ROW(A2:A3))+ROWS(A2)-3 & " sum of invoice "& SUM('Sheet1'!H:H))))
I'm trying to sort out a manifest that needs to be converted to a different format.
Just wondering, is there a way for to find a group of blank cells and delete them, using VBA?
The files look like this:
name
address
address
region
-blank line-
-blank line-
-blank line-
Name
Address
Address
Region
-blank line etc
What I would like is something to delete out the grouped blank cells. The issue I have is sometimes one of the address columns is blank, (one blank line) so I can't just use Go to > Special...
What I would love is if I could work out how to have a VBA to run down the column, pick up a group of 3 or more blank cells on top of each other, and delete those entire rows.
Is that possible?
Cheers, Nigel.
You can always do something like this:
Sub DeleteBlanks()
Dim TotalRows As Long
Dim i As Long
Dim j As Long
TotalRows = Rows(Rows.Count).End(xlUp).Row
For i = TotalRows To 2 Step -1
'Check if there is more than one blank cell
If Cells(i, 1).Value = "" And Cells(i - 1, 1).Value = "" Then
'Count the number of cells that are blank
Do While Cells(i - j, 1).Value = ""
If i - j <= 1 Then
Exit Do
End If
j = j + 1
Loop
'Delete group of blank of cells
If i - j <= 1 Then
Rows(i & ":" & i - j).Delete
Else
Rows(i & ":" & i - j + 1).Delete
End If
j = 0
End If
Next
End Sub
It's been 6 years since I've worked with Excel and i'm a little bit rusty. Here's my scenario:
I am exporting a list of issues to Excel. I need to be able differentiate the associated Link numbers in a cell (mulitple values) from each other. Example, i have two columns,
Key = the number for a ticket
Linked Issues = The Keys associated
I need a statement that would scan the Key column and find a match in the Linked Issues column. Then once the match is found the matching text will assume the font color of the Key.
Where this get complicated is each cell of the Linked Issues column could look something like this iss-3913, iss-3923, iss-1649. So essentially the scan would be for a match within the string. Any help is appreciated.
I am sorry, I don't have time to finish this right now, but wWould something like this help with maybe a loop for each cell in the first column?
Edit: Finished now, second edit to update to B5 and Z5, edit 3 fixed goof with column reference and updated to use variables to assign what column to look in.
Sub colortext()
start_row = 5
key_col = 2
linked_col = 26
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
o = start_row 'start with row one for second column
Do While Not IsEmpty(Cells(o, linked_col)) 'Do until empty cell
If Not InStr(1, Cells(o, linked_col), Cells(i, key_col)) = 0 Then 'if cell contents found in cell
With Cells(o, linked_col).Characters(Start:=InStr(1, Cells(o, linked_col), Cells(i, key_col)), Length:=Len(Cells(i, key_col))).Font
.Color = Cells(i, key_col).Font.Color 'change color of this part of the cell
End With
End If
o = o + 1 'increment the cell in second column
Loop
i = i + 1 'increment the cell in the first column
Loop
End Sub
or maybe
Something like this?
Excel VBA: change font color for specific char in a cell range
This is an old post but I thought I would provide my work around to the conditional formating issue I was having.
Sub colorkey()
start_row = 5
key_col = 2
flag_col = 4
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
Tval = Cells(i, flag_col).Value
Select Case Tval
Case "Requirement"
'cval = green
cVal = 10
Case "New Feature"
'cval = orange
cVal = 46
Case "Test"
'cval = lt blue
cVal = 28
Case "Epic"
'cval = maroon
cVal = 30
Case "Story"
'cval = dk blue
cVal = 49
Case "Theme"
'cval = grey
cVal = 48
Case "Bug"
'cval = red
cVal = 3
Case "NOT MAPPED"
'cval = Maroon
cVal = 1
End Select
Cells(i, key_col).Font.ColorIndex = cVal
i = i + 1 'increment the cell in the first column
Loop
End Sub
Sub colorlinked()
start_row = 5
key_col = 2
linked_col = 26
i = start_row 'start on row one
Do While Not IsEmpty(Cells(i, key_col)) 'Do until empty cell
o = start_row 'start with row one for second column
Do While Not IsEmpty(Cells(o, linked_col)) 'Do until empty cell
If Not InStr(1, Cells(o, linked_col), Cells(i, key_col)) = 0 Then 'if cell contents found in cell
With Cells(o, linked_col).Characters(Start:=InStr(1, Cells(o, linked_col), Cells(i, key_col)), Length:=Len(Cells(i, key_col))).Font
.Color = Cells(i, key_col).Font.Color 'change color of this part of the cell
End With
End If
o = o + 1 'increment the cell in second column
Loop
i = i + 1 'increment the cell in the first column
Loop
MsgBox "Finished Scanning"
End Sub
I'm in the middle of trying to do the above but fail. referring to the image below, i need to insert five empty rows below the 4th row (john lee) and another five empty rows below 7th row (bryan key) and another five below 9th row (casey carton) and so on, i got 30+ groups of different name to do. wondering how to write vba for this? thanks.
So 5 blanks after a change in sorted column A?
Const blanks = 5
Dim lastValue As String, i As Long, r As Long
Do
r = r + 1
If r > 1 And lastValue <> Cells(r, 1).Value Then
If Cells(r, 1).Value = "" Then Exit Do
For i = 1 To blanks
Rows(r).Insert Shift:=xlDown
Next
r = r + blanks
End If
lastValue = Cells(r, 1).Value
Loop
the 1 in Cells() is the column index, i.e. 1 == A