How to do a loop within a loop vba [closed] - vba

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I cannot figure out how to do a loop within a loop.
There is a list of words on sheet 1 that need to be copied and pasted if they match up with any of the 20 desired key words on sheet two, column 1.
This then needs to be copy pasted onto sheet 3. Then I need to look at the same list from sheet 1 and copy paste those onto sheet 4 if they match up with any of the key words from sheet 2, column 2. I could use any help.

Single loop
Dim i As Integer
For i = 1 To 6
Cells(i, 1).Value = 100
Next i
Double Loop
Dim i As Integer,
Dim j As Integer
For i = 1 To 6
For j = 1 To 2
Cells(i, j).Value = 100
Next j
Next i
Good Luck

You don't need 2 loops (which are slow)
Loop through the values in sheet 1 testing if they exists using this:
If WorksheetFunction.CountIf(Sheets("Sheet2").Range("A1:A20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 3
ElseIf WorksheetFunction.CountIf(Sheets("Sheet2").Range("B1:B20"), Range("A1")) > 0 Then
'Your copy and paste code goes here for sheet 4
End If

Related

Cut n cells and insert to below row using excel vba [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying cut 4 adjacent cells and insert a new row below and paste it on a new row.
My input is similar to
I have 4 columns Addr,Phone,Count,Amount. Some Rows of my excel sheet contains multiple n numbers of entries. I want to cut multiple of 4 cells and insert a new row below and paste it on.
The output would be similar to
I tried with transform function but unable to produce the expected result.
How can I do this with vba code or any excel functions
Here is the code its exactly work with your requirement
Sub Narasappa()
For i = 2 To 1000
If ThisWorkbook.Worksheets(5).Cells(i, 2) = "" Then
Exit For
End If
For j = 6 To 1000 Step 4
If ThisWorkbook.Worksheets(5).Cells(i, j).Value = "" Then
Exit For
Else
ThisWorkbook.Worksheets(5).Cells(i, j).Resize(, 4).Cut
ThisWorkbook.Worksheets(5).Range("B" & i + 1).Insert xlShiftDown
End If
Next
Next
End Sub

VBA - Add Sheets based on a value and name accodingly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am having trouble with my code. I need to create new sheets and name them according to the name of a machine. Which is the range "B" & (12 * x - 5). Yes the range changes based on x which in this example is equal to 1 to 33, and is within the proper For statement. However my code is creating a large number of sheets and not just 11, in this example. Sheets("Tool Setup").Range("C18") = 11 in this example. Also, my intention is to name these sheets according to the value in Range "B" & (12 * x - 5).
Dim Sheetcnt As Integer, Tabs As Integer
Sheet = ThisWorkbook.Names(Sheets("Reporting").Range("B" & (12 * x - 5))).RefersToRange.Value
Sheetcnt = Sheets("Tool Setup").Range("C18")
For Tabs = 1 To Sheetcnt
Sheets.Add After:=Sheets(Sheets.Count) 'creates a new worksheet
Sheets(Sheets.Count).Name = Sheet 'renames the new worksheet
Next Tabs
If someone can help me figure out how to create a code that will satisfy this problem, it would be much appreciated. I have a hunch it has to do with the variable Tabs.
Thank you in advance.
Based on what you've provided, I've come up with the code below which names 11 sheets according to a formula similar to yours. To demonstrate that it works, I placed consecutive numbers in the first 127 rows of Column B of the active sheet. Then, the code calculates which row to grab using the formula Range("B" & (12 * i - 5) That formula gives the following sequence: 7 19 31 43 55 67 79 91 103 115 127, and so 11 sheets with those numbers are made.
If this is not what you intended, perhaps you can use this working code as a place from which to start. Or, better yet, provide more definition for your question.
Option Explicit
Sub sheetNamer()
Dim num As Integer, i As Integer, sh As Worksheet
num = 11
Set sh = ActiveSheet
For i = 1 To num
Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = sh.Range("B" & (12 * i - 5))
Next i
End Sub

VBA code to transpose one multiple data column into several columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Hello I have a list of 65000 rows of stocks price for one column, and I would like to adapt it like in the second pic, anyone has any idea how to code it with vba ? Thank you!
Assuming that the data structure is always the same (12 months of data and 3 rows for data set id's). Change Sheet Name on Code Line 9 to suit your Sheet Name
Sub TRANSPOSE_DATA()
Dim lRow, i, x As Long
'------------------
With ThisWorkbook
'ADD OUTPUT WORKSHEET
.Sheets.Add After:=.Sheets(.Sheets.Count) 'add a sheet for output
.ActiveSheet.Name = "OUTPUT" 'sheet rename
'COPY DATA
With .Sheets("Hoja1") 'Change sheet name for yours
lRow = .Range("A1048576").End(xlUp).Row 'last row definition
.Range("A1:B15").Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Range("A1") 'First dataset copy (including headers)
x = 3 'first iteration column definition
For i = 16 To lRow - 14
.Range("B" & i & ":B" & i + 14).Copy Destination:=ThisWorkbook.Sheets("OUTPUT").Cells(1, x) 'copy data from each iteration
x = x + 1 'transpose 1 column for next iteration
i = i + 14 'transpose 12 months plus header rows for next iteration
Next i
End With
End With
End Sub

Merging Excel cells in between two columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am Working with many Excel files. The first column is fixed with the heading product-name. The other column name is price.
I am trying to merge the columns that are between these two columns, Each file contains different number of columns between these two columns product-name and price
I am having trouble writing a macro to run each Excel file, can anyone show me an example?
I am assuming that only 1 st row need to be checked. assuming these are headers.
You can use the below VBA to check the the column headers and merge the in between columns
I have used random variables, you may change accordingly.
Sub test()
Cells(1, 1).Select
j = 1
Do
k = Cells(1, j).Value
Cells(1, j).Select
j = j + 1
Loop Until (k = "Product-name")
c1 = j
Do
k = Cells(1, j).Value
Cells(1, j).Select
j = j + 1
Loop Until (k = "Price")
c2 = j - 2
If (c2 > c1) Then
Range(Columns(c1), Columns(c2)).Select
Selection.Merge
End If
End Sub
k variable will store the column value and will be compared to "Product-name" and "Price".
c1 will store the next column number of the "Product-name"
c2 will store the column number of the "Price" -1, in logic it advances by 2 so subtracting the column number with 2.
Now you have column numbers in between both the headers.
Here there is small scenario which need to be considered, if there are no columns or if there is at least one column between these 2 columns, in that case directly you cannot merge columns. so additional comparison of column values.
I hope this answers your query. The code is tested and is working fine.

Searching for a particular value then pasting into another workbook [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
Let me give you a basic understanding of my what I'm trying to do. I have two workbooks: Master Workbook and Workbook A. Information in Workbook A will be inputted into the Master Workbook. In Workbook A, there is Column X with numbers between the ranges of 1 to 25. All I care about are values greater than 14.
Problem: How do I create a VBA function that looks at Column X (Row 1) to see if it is greater than 14? If it is then it copies the entire row and pastes it into the Master Workbook, else it moves onto Column X2. Also, after copying row 1 and pasting into Master Workbook, I also need it to go back to Workbook A and check the rest of Column X if it is greater 14.
Thank you so much in advance!
This code should do what you want:
Private Sub checkAndCopy()
Dim i As Integer
Dim lastRow As Integer
Dim foundItem As String
Dim j As Integer
Dim pasteTo As Range
Dim k As Integer
k = 0
lastRow = WorksheetFunction.CountA(Range("A:A"))
For i = 1 To lastRow
If Cells(i, 24).Value > 14 Then
k = k + 1
For j = 1 To 24
foundItem = Cells(i, j).Value
Set pasteTo = Workbook(yourWorkbook).Cells(k, j)
pasteTo.Value = foundItem
Next j
End If
Next i
End Sub
Note that it copies into the new workbook starting on Row 1. You can have it search for the next empty line and add to that by including:
k = Workbook(yourWorkbook).WorksheetFunction.CountA(Range("A:A")) + 1
I hope this helps!