EXCEL: Automating spreadsheet data input [closed] - vba

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I am currently doing some data entry for a spreadsheet which contains hundreds on entries and want to automate the process, I have a good idea of what I want it to do but have little experience with Excel or VBA.
The idea behind it is that I have a code in one column and in the next column there is another code which is unique to the value in the former column. To give an example:
So for every cell that contains 123, the column next to it will be "ABC".
The sort of solution I would like is a macro that will work its way down Column A, storing the value of each cell (or something of that effect) and then working its way down to check for values that match that stored one. If a match is found, the macro will then copy the code from column B, the cell that is next to the stored cell and copy it into the cell in column B, next to the match.
EXAMPLE:
It will store the "123" value in A, work its way down Column A to find other cells matching "123" and when it finds them copy "ABC" into the column B cells next to the matches.
Hope this is easy to understand and someone can help me with coming up with a solution, would make this whole process alot easier as the spreadsheet is growing by the day and manual input is taking far to much time

Give this macro a try:
Sub FillInTheBlanks()
Dim rA As Range
Dim rB As Range
Dim r As Range, rr As Range
Dim N As Long
Dim va As Variant
N = Cells(Rows.Count, "A").End(xlUp).Row
Set rA = Range("A1:A" & N)
Set rB = rA.Offset(0, 1).Cells.SpecialCells(xlCellTypeBlanks)
If rB Is Nothing Then Exit Sub
For Each r In rB
va = r.Offset(0, -1).Value
For Each rr In rA
If rr.Value = va And rr.Offset(0, 1) <> "" Then
r.Value = rr.Offset(0, 1).Value
End If
Next rr
Next r
End Sub

Related

How to get the cell value from another sheet using vba [closed]

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.

VBA to return nth row number from a filtered table in excel [closed]

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 7 years ago.
Improve this question
Can anyone please help me with finding the absolute row number of nth element after filter is applied in an excel table.
For example, I have filter on and have a visible range of data element. Now 20th (nth) row in this filtered range could be 60th row (absolute sense) when no filters are on. Is there a way to find the absolute row number using VBA?
Simplest method is special cells. See below:
Sub test()
Dim i As Integer, j As Integer, k As Integer
Dim Report As Worksheet
Set Report = Excel.ActiveSheet ' Store the current worksheet in a variable (always a good idea)
Dim visRng As Range ' Creating a range variable to store our table, excluding any rows that are filtered out.
Set visRng = Report.UsedRange.SpecialCells(xlCellTypeVisible) ' Select only rows within the used range that are visible.
Dim r As Range
For Each r In visRng.Rows ' Loop through each row in our visible range ...
MsgBox (r.Row) ' ... and retrieve the "absolute" row number.
Next
End Sub
EDIT
Tom claims this method will not work, but I'm pretty sure it does what you ask. Example:
Here is my original test table--unfiltered so you can see what we're doing.
And then we filter a couple values somewhere in the middle of the table...
Now when we run the script I posted above, our message box will show the "absolute" row number for each unfiltered row. Results are 1,3,4,5, and 7.
As a function I suggest
Function RowNum(Target As Range) As Long
RowNum = Target.Row
End Function
use by entering in a cell =RowNum(E9). If you want the line relative to the table start and your table starts in -say- row 21, just subtract this from the result (you can use the same function to find the row of table start or course) ... e.g. =rownum(A2)-rownum($A$1) ... mind the absolute notation of table header!
If you don't like this as a function, you could use the SelectionChange event to display the row number of the selected cell in the message line (optionally depending on a "debug flag" somewhere in your sheet).
Tne non-VBA approach would be to use the CELL formula ... e.g. =CELL("row",A1)
The answer brought up by #Lopsided wont work, if there are other hidden cells after the nth entry. They would then be added to the absolute position.
This'll work, you have to change n by yourself in the script. Changing that shouldn't be to hard. If you have any questions regarding that, feel free to ask. (;
Sub absoluteRowID()
Dim RowCount, hiddenRows As Integer
'relative position n
n = 5
i = 0
Do While i < n
i = i + 1
If ThisWorkbook.Sheets(1).Rows(i).EntireRow.Hidden Then
'if there is a hidden row, position is incremented
n = n + 1
End If
'if there is no hidden row, nothing happens
Loop
MsgBox (i)
End Sub
HTH

Excel VBA: locate details in one sheet, then copy (append) to another [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 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

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!

VBA Excel- how to know the layout of data in a range [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 8 years ago.
Improve this question
I am an Excel VBA newbie, though I have several years of programming experience.
I am trying to write a VBA function in excel where a user can input several cells in an application.inputbox by selecting individual cells, a cell range, or a combination of both. Once input, I want to check the values for errors etc.
I can input the range without problem, but seem to need a way to inspect the range to determine its layout so I can find the values. It seems the location of the values varies with how the user selected the cells.
Suppose the user selects 4 cells. A1, A2, c2, f5 vs drag selecting A1:A2, and then clicking C2,f5.
in both cases Inputs.Count is 4.
In the first case Inputs(1)- inputs(4) do not return the selected values.
Here, in the first case, the values appear as single cells in 4 different Areas.
In the second case, Inputs(1)-Inputs(4) do return the selected values, yet these appear as three areas. One area with two cells (two rows, one column), and two areas with one cell each.
I've two questions-
1: Is there an easy way to determine where the data I want to inspect are (i.e., the layout of the range) without checking all the area/cell counts?
2: Where can I find an accessible read on using ranges.
Best,
Byron
To compare a few methods after selecting A1,A2,B3,D4
Sub tester()
Dim c As Range, a As Range, x As Long, s
Debug.Print Selection.Areas.Count
Debug.Print "###By cell count"
s = ""
For x = 1 To Selection.Cells.Count
s = s & " " & Selection.Cells(x).Address()
Next x
Debug.Print s
Debug.Print "###For Each cell"
s = ""
For Each c In Selection.Cells
s = s & " " & c.Address()
Next c
Debug.Print s
Debug.Print "###Looping through areas"
s = ""
For Each a In Selection.Areas
For x = 1 To a.Cells.Count
s = s & " " & a.Cells(x).Address()
Next x
Next a
Debug.Print s
End Sub
Output:
###By cell count
$A$1 $A$2 $A$3 $A$4
###For Each cell
$A$1 $A$2 $B$3 $D$4
###Looping through areas
$A$1 $A$2 $B$3 $D$4
Only method #1 is unreliable - the others give consistent results however the range was originally selected.