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
Related
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
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Hi I am working on Excel vba i've a set of columns retrived from sql into excel using vba and i've to get the id of the particular retrived column value by referring another sheet. For example i've 2 sheets where sheet1 has rid,rname columns and sheet2 has sid,rid,date columns.whereas, The values retrived from sql to excel sheet2 has sid,rname,date values i want to replace rname value with rid value by referring corresponding rid in sheet1. How to do this using VBA code
Please consider all comments under your question for further posts.
Here is an answer nevertheless.
This works for two columns with the same amount of rows:
Sub stack()
Dim ws1, ws2 As Worksheet
Dim targetRng As Range
Dim i As Long
Set ws1 = Worksheets(1)
Set ws2 = Worksheets(2)
Set targetRng = ws2.Range(ws2.Cells(firstRow, columnID), ws2.Cells(lastRow, columnID))
i = firstRowSourceSheet
For Each cell In targetRng
cell.Value = ws1.Cells(i, columnIDSource)
i = i + 1
Next
End Sub
Fill in variables accordingly.
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
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 8 years ago.
Improve this question
I would really appreciate your help with building some macro logic. I've messed around modifying recorded macros in the past, but am not familiar enough with VBA to build something from scratch. I've looked over the many examples here as well (mindboggling!), but did not find anything close enough to what I need to do...
I know it probably only takes a few lines of code - which makes it even more frustrating. Can someone please put me on the right track?
I have a single workbook with two sheets. Sheet1 contains a long list (~25k rows, but variable) of product details. Column A has the product ID, then columns B through G hold specific details on each product. Sheet2 is similar, again with product ID in column A and (different) properties in columns B through E. Sheet2 is much smaller, at about 100 rows (also variable).
What I need to do is to loop through the products (rows) in Sheet2, find the corresponding Product ID in Sheet1, and copy/paste the product properties in Sheet1 (B through G) to Sheet2 (to the right of the existing properties, so starting at column F in my example) - effectively merging all product properties in Sheet2.
I would be very grateful if one of you wizzards can provide skeleton code for that....
Set the two ranges from your two sheets, loop through them and find matches in product id, then just copy the values from the first sheet into the second one. Also replace the ranges to match your conditions.
Sub search()
Dim cell As Range, rng As Range, rng2 As Range, cell1 As Range, n As Integer, m As Integer
Set rng = Sheet1.Range("A2:A9")
Set rng2 = Sheet2.Range("A2:A3")
n = 1
m = 1
For Each cell In rng
n = n + 1
For Each cell1 In rng2
m = m + 1
If cell.Value = cell1.Value Then
Sheets("Sheet2").Range("F" & m & ":J" & m).Value = Sheets("Sheet1").Range("B" & n & ":F" & n).Value
End If
Next cell1
m = 1
Next cell
End Sub
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!