Do Until IsEmpty to loop through multiple user-defined ranges - vba

This is a continuation of the question Do Until IsEmpty to loop through the user-defined ranges:
In the code below the excel Macro loops through Original text range, replacing all the instances of "tagname" with the assigned loopText in cell D2 and puts the corrected text into cell B9. Then it checks whether the looptext range has other values and loops through the original text again to account for these. Once the looptext cell is empty, the macro stops and prints the corrected text for each instance of loopText.
Parallel to replacing "tagname" in cells D2 and onward I want the Macro to replace the word "sheetname" with the range of cells in E2 and onward.
I have created a separate Do Until IsEmpty loop, however excel only executes the first one.
Please, help locate an error in my VB code.
Thank you.
Sub CommandButton21_Click()
Dim correctedText2 As Range
Dim OriginalText2 As Range
Dim loopText1 As Range
Dim loopText2 As Range
Dim cel As Range
Dim i As Long
Dim j As Long
Dim k As Long
Set OriginalText2 = Range("H3:H20")
Set correctedText2 = Range("B9")
Set loopText1 = Range("D2")
Set loopText2 = Range("E2")
i = 0
j = 0
Do Until IsEmpty(loopText1.Offset(j).Value)
For Each cel In OriginalText2
correctedText2.Offset(i).Value = Replace(cel.Value, "tagname", loopText1.Offset(j).Value)
i = i + 1
Next cel
j = j + 1
Loop
k = 0
Do Until IsEmpty(loopText2.Offset(k).Value)
For Each cel In OriginalText2
correctedText2.Offset(i).Value = Replace(cel.Value, "sheetname", loopText2.Offset(k).Value)
i = i + 1
Next cel
k = k + 1
Loop
End Sub

Thanks to #ScottHoltzman, here's the answer!
Sub CommandButton21_Click()
Dim correctedText1 As Range
Dim correctedText2 As Range
Dim correctedText3 As Range
Dim OriginalText1 As Range
Dim OriginalText2 As Range
Dim loopText1 As Range
Dim loopText2 As Range
Dim cel As Range
Dim i As Long
Dim j As Long
Set OriginalText2 = Range("H3:H20")
Set correctedText2 = Range("B9")
Set loopText1 = Range("D2")
Set loopText2 = Range("E2")
i = 0
j = 0
Do Until IsEmpty(loopText1.Offset(j).Value)
For Each cel In OriginalText2
correctedText2.Offset(i).Value = Replace(Replace(cel.Value, "tagname", loopText1.Offset(j).Value), "sheetname", loopText2.Offset(j).Value)
i = i + 1
Next cel
j = j + 1
Loop
End Sub
Instead of creating two Do Until IsEmpty Loops, combine into one loop with a double replace.

Related

VBA: repeat a value a number of times

I have an input table in range A2: D6, I want each value in input range to be shown 4 times in output
range A8: D27
Below is the code it is copying only one value to the output range.
Could you please guide me on how to change this VBA code to get the desired result as shown in the below picture.
Sub RepeatData()
Dim Rng As Range
Dim lr As Integer
Dim C As Integer
Range(("A8"), Range("D10000")).ClearContents
lr = Range(("A1"), Range("A2").End(xlDown)).Count
C = 4
Dim InputRng As Range, OutRng As Range
Set InputRng = Range("A2", Range("d" & lr))
Set OutRng = Range("a8")
For Each Rng In InputRng.Rows
xValue = Rng.Range("A1", "a1").Value
xNum = 4 ' No. of times to repeat
OutRng.Resize(xNum, C).Value = xValue
Set OutRng = OutRng.Offset(xNum, 0)
Next
End Sub
I can see that you are looping through entire input rows, so I would propose a solution copying entire rows.
Sub RepeatData()
Dim inputWorksheet As Worksheet
Dim inputRange As Range
Dim outputRangeStart As Range
Dim rowToBeCopied As Range
Dim firstFreeRow As Range
Dim numberOfOutputRows As Integer
Dim outputRange As Range
' set all params
Set inputWorksheet = Sheet1
Set inputRange = inputWorksheet.Range("A2:D5")
Set outputRangeStart = inputWorksheet.Range("A8")
numberOfOutputRows = 4
' clean output range
Range(outputRangeStart, outputRangeStart.End(xlDown).End(xlToRight)).ClearContents
' copy values
For Each rowToBeCopied In inputRange.Rows
Set firstFreeRow = FindFirstFreeCell(outputRangeStart)
Set outputRange = ExtendRangeByNumberOfRows(firstFreeRow, numberOfOutputRows)
rowToBeCopied.Copy
outputRange.PasteSpecial xlPasteValues
Next rowToBeCopied
End Sub
Function FindFirstFreeCell(startFrom As Range) As Range
If IsEmpty(startFrom) Then
Set FindFirstFreeCell = startFrom
ElseIf IsEmpty(startFrom.Offset(1, 0)) Then
Set FindFirstFreeCell = startFrom.Offset(1, 0)
Else
Set FindFirstFreeCell = startFrom.End(xlDown).Offset(1, 0)
End If
End Function
Function ExtendRangeByNumberOfRows(rangeToBeExtended As Range, numberOfRows As Integer) As Range
Set ExtendRangeByNumberOfRows = Range(rangeToBeExtended, rangeToBeExtended.Offset(numberOfRows - 1, 0))
End Function

Find the texts in Dynamic Range in another sheet

I am creating a VBA application that will find the text that I have entered in a certain range (Should be dynamic, in order for me to input more in the future). With that the entered texts in the range will look for the words in another sheet column:
Example:
And it will look for the words inputted in another sheet.
Dim Main as Worksheet
Set Main = Sheets("Sheet1")
Dim Raw2 as Worksheet
Set Raw2 = Sheets("Sheet2")
LookFor = Main.Range(D8:100)
Fruits = Raw2.Range("G" & Raw2.Rows.Count).End(xlUp).row
For e = lastRow To 2 Step -1
value = Raw2.Cells(e, 7).value
If Instr(value, LookFor) = 0 _
Then
Raw2.Rows(e).Delete
Honestly I am not sure how to proceed. And the mentioned code is just experiment. Desired output is to delete anything in sheet2 except for the rows that contain the words that I have inputted in the "Look for the words". Hope you can help me. Thank you.
This should do the trick :
Sub Sevpoint()
Dim Main As Worksheet
Set Main = Sheets("Sheet1")
Dim Raw2 As Worksheet
Set Raw2 = Sheets("Sheet2")
Dim LooKFoR() As Variant
Dim LastRow As Double
Dim i As Double
Dim j As Double
Dim ValRow As String
Dim DelRow As Boolean
LooKFoR = Main.Range(Main.Range("G8"), Main.Range("G" & Main.Rows.Count).End(xlUp)).Value
LastRow = Raw2.Range("G" & Raw2.Rows.Count).End(xlUp).Row
For i = LastRow To 2 Step -1
ValRow = Raw2.Cells(i, 7).Value
DelRow = True
'MsgBox UBound(LooKFoR, 1)
For j = LBound(LooKFoR, 1) To UBound(LooKFoR, 1)
If LCase(ValRow)<>LCase(LooKFoR(j, 1)) Then
Else
DelRow = False
Exit For
End If
Next j
If DelRow Then Raw2.Rows(i).Delete
Next i
End Sub

Excel VBA array of Selected Range

I know how to make two functions on each column (in this case TRIM and STRCONV to ProperCase
Dim arrData() As Variant
Dim arrReturnData() As Variant
Dim rng As Excel.Range
Dim lRows As Long
Dim lCols As Long
Dim i As Long, j As Long
Range("H2", Range("H2").End(xlDown)).Select
lRows = Selection.Rows.Count
lCols = Selection.Columns.Count
ReDim arrData(1 To lRows, 1 To lCols)
ReDim arrReturnData(1 To lRows, 1 To lCols)
Set rng = Selection
arrData = rng.Value
For j = 1 To lCols
For i = 1 To lRow
arrReturnData(i, j) = StrConv(Trim(arrData(i, j)), vbProperCase)
Next i
Next j
rng.Value = arrReturnData
Set rng = Nothing
Currently I'm trying to figure out how to add one more FOR which where I could gather more than one selection ranges for example:
Set myAnotherArray(0) = Range("H2", Range("H2").End(xlDown)).Select
Set myAnotherArray(1) = Range("J2", Range("J2").End(xlDown)).Select
For k = 1 To myAnotherArray.lenght
Because I'm copying and pasting whole script to make aciton on three columns. Tried already:
Dim Rng As Range
Dim Area As Range
Set Rng = Range("Range("H2", Range("H2").End(xlDown)).Select,Range("J2", Range("J2").End(xlDown)).Select")
For Each Area In Rng.Areas
Area.Font.Bold = True
Next Area
Even tried to Union range but I failed. Any sugesstions?
And as always... Thank you for your time!
I found a way you could use to perform work on those ranges, refer to the code below:
Sub DoSomethingWithRanges()
Dim m_Worksheet As Excel.Worksheet
Dim m_Columns() As Variant
Set m_Worksheet = ActiveSheet
' fill all your columns in here
m_Columns = Array(2, 3, 4)
For Each m_Column In m_Columns
' the area being used ranges from the second until the last row of your column
With m_Worksheet.Range(m_Worksheet.Cells(2, m_Column), m_Worksheet.Cells(m_Worksheet.UsedRange.Rows.Count, m_Column))
' do things with range
.Font.Bold = True
End With
Next m_Column
End Sub
In the variant array m_Columns you can add all the columns you want. Only downside is that in my example you have to use numbers to specify columns instead of "H". However, you don't have to worry about the row-indexes, since the area automatically ranges from the second to the last used row.

Do Until IsEmpty to loop through the user-defined ranges

This is a continuation of the discussion Looping through values in cells to replace in the text
The following code take the value in cell D2 and populates its value in the OriginalText in all instances where the word "tagname" is used.
Then it loops through the loopText range and produces the correctedText range.
I am looking to use Do UntilEmpty command loop so that the loopText range depends on the user range input, say if the user populates cells D2:D54 then all of then are executed.
Below is my code, but I keep getting a run-time error '91'
Object variable or With block variable not set
Please, help.
Thanks.
Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim loopText As Range
Dim i As Long
Dim cel As Range
Dim cel2 As Range
Do Until IsEmpty(loopText.Value)
Set correctedText = Range("B10")
Set OriginalText = Range("H3:H20")
Set loopText = Range("D2")
i = 0
Columns(2).Clear
For Each cel2 In loopText
For Each cel In OriginalText
correctedText.Offset(i).Value = Replace(cel.Value, "tagname", cel2.Value)
i = i + 1
Next cel
Next cel2
Loop
End Sub
Try this:
You only need the two loops, The first loop was changed to do what you are looking for.
Sub CommandButton21_Click()
Dim correctedText As Range
Dim OriginalText As Range
Dim loopText As Range
Dim i As Long
Dim cel As Range
Dim cel2 As Range
Dim j As Long
Set correctedText = Range("B10")
Set OriginalText = Range("H3:H20")
Set loopText = Range("D2")
i = 0
j = 0
Columns(2).Clear
Do Until IsEmpty(loopText.Offset(j).Value)
For Each cel In OriginalText
correctedText.Offset(i).Value = Replace(cel.Value, "tagname", loopText.Offset(j).Value)
i = i + 1
Next cel
j = j + 1
Loop
End Sub

Creating a separate excel using Macro

I am having a excel with one column that has got information regarding tender. Each cell will have a value like
Column: Nokia([Mode1.Number],OLD)
Column: Motorola([Mode1.Number],OLD)
Column: Motorola([Mode2.Number],NEW)
Column: Motorola([Mode3.Number],OLD)
Column: Samsung([Mode2.Number],NEW)
I need to create 2 excel out of this. One should 've all the information of the OLD and the second excel should've all the information of NEW.
So my output excel should contain
First Excel
Nokia([Model1.Number])
Motorola([Mode1.Number])
Motorola([Mode3.Number])
Second Excel
Motorola([Mode2.Number])
Samsung([Mode2.Number])
Kindly help me.. Thanks in advance..
Highlight the cells containing the data you want to copy and then run this code
sub copystuff
dim r as range
dim tn as range
im to as range
dim wsNewTarget as worksheet
dim wsOldTarget as worksheet
dim wsSource as worksheet
set wsSource = activesheet
set wsNewtarget = activeworkbook.worksheets.add
set wsoldtarget = activeworkbook.worksheets.add
set tn = wsnewtarget.range("a1")
set to =wsoldtarget.range("a1")
for each r in wssource.selection
if imstr(r,"NEW")>0 then
tn=r
set tn = tn.offset(1,0)
else
to=r
set to = to.offset(1,0)
end if
next r
end sub
Sub SplitOldNew()
Dim InRange As Range, OldRange As Range, NewRange As Range
Dim Idx As Integer
Set InRange = Selection ' select all cells to be split
Set OldRange = Worksheets("OLD").[A1] ' choose appropriate target entry points
Set NewRange = Worksheets("NEW").[A1] ' ...
Idx = 1 ' loop counter
Do While InRange(Idx, 1) <> ""
If InStr(1, InRange(Idx, 1), "OLD") <> 0 Then
DBInsert OldRange, InRange(Idx, 1)
Else
DBInsert NewRange, InRange(Idx, 1)
End If
Idx = Idx + 1
Loop
End Sub
Sub DBInsert(intoRange As Range, Arg As String)
Dim Idx As Integer
Idx = 1 ' loop counter
Do While intoRange(Idx, 1) <> "" ' find first blank row
Idx = Idx + 1
Loop
intoRange(Idx, 1) = Arg ' write out
End Sub