I want to go into a sheet and look for a value. If the value is there I want to grab the data in its row and paste transposed in another sheet. This function is working.
However, if the value in the column is not there I want to paste filler text into the column where the data would otherwise go.
I am getting a "Type Mismatch" error when I run the following code. What is going wrong/ how can I made this happen.
Dim c As Long
Windows("WBGrab").Activate '-> opens doc we want to look a
c = Sheets("SheetName").Columns(2).Find(What:="Commercial Income", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False).Row
If c Is Nothing Then
Windows("WBPaste").Activate
Range("C2:C7").Value = "-"
Else
Sheets("SheetName").Range("C" & c & ":H" & c).Select '-> opens MBI DSCR sheet and copes naming & values
Selection.Copy '-> copies the selected area
Windows("WBPaste").Activate '-> opens back up the data lake
Sheets("Sheet1").Range("C" & 2).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
xlNone, SkipBlanks:=False, Transpose:=True
End Sub
If Find() fails to make a match then your code will error before getting to If c Is Nothing Then because of the .Row tagged onto the Find() line.
Something like this should work:
Sub Tester()
Dim c As Range, wsSrc As Worksheet, wsDest As Worksheet, cDest As Range
'set up source and destination sheets
Set wsSrc = Workbooks("WBGrab").Worksheets("SheetName") 'add the file extension to the name
Set wsDest = Workbooks("WBPaste").Worksheets("Sheet1")
Set c = wsSrc.Columns(2).Find(What:="Commercial Income", LookIn:=xlValues, _
LookAt:=xlPart, MatchCase:=False)
Set cDest = wsDest.Cells(Rows.Count, "C").End(xlUp).Offset(1, 0) 'next paste destination. C2?
If c Is Nothing Then 'didn't make a match with Find() ?
cDest.Resize(1, 7).Value = "-" 'fill placeholders
Else
c.Offset(0, 1).Resize(1, 7).Copy 'got match - copy range
cDest.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End If
End Sub
Related
I want to search a specified column for a String value. Then I want to grab the six values to the right of that column value. I am currently doing it like this which is working correctly.
Set c = wsSrc.Columns(col).Find(What:="DSCR Analysis", LookIn:=xlValues, _
LookAt:=xlPart, MatchCase:=False)
Note here are the declarations for the following variables.
Property Get wsSrc() As Worksheet
Set wsSrc = ActiveWorkbook.Sheets("MBI DSCR")
End Property
Property Get wSrc() As Workbook
Set wSrc = ActiveWorkbook
End Property
Property Get col() As Long
col = wsSrc.Columns.Find(What:="MBI", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
End Property
Property Get col2() As Long
col2 = wSrc.Sheets("Inputs").Columns.Find(What:="Deal Team", LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False).Column
End Property
Property Get wsDest() As Worksheet
Set wsDest = Workbooks("LBM_DSCT_DataLake.xlsm").Worksheets("Sheet1")
End Property
Currently I am copying a specified value from the source sheet then pasting it to the destination sheet over and over for all the different values I want to copy.
However I am wondering if it is possible to copy all the different values (ie "DSCR Analysis") to a clipboard and then paste. So the program does not "jump" back and forth between sheets rather grabs all the data it needs once, copies it, then pastes it in the destination sheet.
Below is an example of how one of my functions works wit hthe copying and pasting. I have around 20 of these where the program jumps back and forth.
Sub PasteCI()
Dim c As Range, cDest As Range
Set c = wsSrc.Columns(col).Find(What:="Commercial Income", LookIn:=xlValues, _
LookAt:=xlPart, MatchCase:=False)
Set cDest = wsDest.Cells(Rows.Count, "E").End(xlUp).Offset(1, 0) 'next paste destination. C2?
If c Is Nothing Then 'didn't make a match with Find() ?
cDest.Resize(6, 1).Value = "-" 'fill placeholders
Else
c.Offset(0, 1).Resize(1, 6).Copy 'got match - copy range
cDest.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, _
Operation:=xlNone, SkipBlanks:=False, Transpose:=True
End If
End Sub
I am interested in doing this: IF Cell C14 = Cell $C$8, THEN keep the cell value BUT remove the IF formula. For rest of the Cells in Column C with IF Formula (False condition: "InsertText!"), retain the IF formula
Refer to Image here
I've tried multiple ways of phrasing the VBA syntax but I'm not getting the desired result.
Sub convertToValue()
Dim totRow As Long
Dim rng As Range
' Find row with word InsertText in it
totRow = Cells.Find(What:="InsertText", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Row
Set rng = Rows("C:C")
rng.Copy
rng.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, Transpose:=False
ActiveSheet.Paste
Application.CutCopyMode = False
End Sub
Is there a way to do this? Thanks!
This works for me with my test spreadsheet.
Sub convertToValue()
LastRow = ActiveCell.SpecialCells(xlLastCell).Row
' change the 13 in the line below to whichever row the data
' actually starts on (the one after the table header)
For Row = 13 To LastRow
Set CurrentCell = Range("C" & Row)
' do the copy/paste only if the current cell is a
' formula AND it doesn't evaluate to "Insert Text"
If Left(CurrentCell.Formula, 1) = "=" And CurrentCell.Value2 <> "Insert Text" Then
Range("C8").Copy
CurrentCell.PasteSpecial Paste:=xlPasteValues
End If
Next
Range("C3").Select
End Sub
If you assign it to a button then you can set the product, enter new details, push the button, and repeat.
'Declare Variables
Dim rng As Range
Dim AmQ As Worksheet, UpIss As Worksheet, QuMa As Worksheet
'Set your sheet names into variables for easier referencing
Set AmQ = Sheets("AMEND QUOTE")
Set UpIss = Sheets("UPISSUE")
For i = 7 To 57 '7 = Column H, 8 = Column G, etc.
'Set the address of the found value to the rng variable
Set rng = UpIss.Cells.Find(What:=AmQ.Cells(4, i).Value, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rng Is Nothing Then 'CHECK IF THE SEARCH TERM (FROM QUOTE SHEET) WAS FOUND IN THE TARGET SHEET (ESTIMATE)
If Not rng = "" Then 'CHECK IF THE SEARCH TERM WAS A ZERO LENGTH STRING
rng.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(14, 0)).Copy 'Copy the cell 41 rows down and 3 columns across
'QuMa.Cells(4, i).Offset(14, 0).PasteSpecial Paste:=xlPasteValues 'Paste into the cell 14 rows below the original search term in the QUOTE sheet
Sheets("QUOTE MASTER").Select
Range("C5000").End(xlUp).Select
ActiveCell.Offset(1, 0).Select
'Copy Formats
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=True
'Copy Values
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
'Autofit
Columns("C:Q").EntireColumn.AutoFit
Application.CutCopyMode = False
ElseIf rng = "" Then 'EXIT SUB IF SEARCH TERM WAS A ZERO LENGTH STRING
'MsgBox "Work is Done"
Exit Sub
End If
ElseIf rng Is Nothing Then 'EXIT SUB IF SEARCH TERM WAS NOT FOUND IN THE TARGET SHEET
'MsgBox "Work is Done"
Exit Sub
End If
Set rng = Nothing
Next i 'Move to the next column across and loop
This is where I am getting my error:
rng.Offset(1, 0).Select
Range(ActiveCell, ActiveCell.Offset(14, 0)).Copy
not able to copy in the loop.
I have Sheet1 which is a form with fields where we enter data to be fed in the database (Sheet2).
Ideally, here's what I want it to do:
I want to search a field/record using the form contents in Sheet1, then search for that term on Sheet2. If it doesn't exist on Sheet2, give me a pop up message saying data doesn't exist.
If it does exist in Column A on Sheet2, then select the cell to the right of the result (Column B). Then paste that cell's contents in relevant fields on Sheet1
Then continue until all of the fields on Sheet1 has been searched for on Sheet2.
Here's the code I've been using. It only works for about 5 lines before it comes up with an error. Any help would be greatly appreciated.
I really don't want the MsgBox to pop up at all.
Sub abc()
Do Until IsEmpty(ActiveCell)
Dim MyString As String
MyString = ActiveCell
Sheets("Sheet2").Select
Set RangeObj = Cells.Find(What:=MyString, After:=ActiveCell, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If RangeObj Is Nothing Then MsgBox "Not Found" Else: RangeObj.Select
ActiveCell.Offset(0, 1).Range("A1").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Sheet1").Select
ActiveCell.Offset(0, 1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(-1, -1).Select
Loop
End Sub
Please let me know what I'm doing wrong.
You were on the right path.. just a few amendments to how you were copying the data.
Option Explicit
Sub sheet2_lookup()
Dim strVal As String
Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet, RangeObj As Range
Set wb = ThisWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")
Cells(1, 1).Activate '' amend this to your starting cell
While ActiveCell.Value2 <> ""
strVal = ActiveCell.Value2
Set RangeObj = ws2.Columns("A").Find(What:=strVal, After:=Cells(1, 1), LookIn:=xlValues)
If RangeObj Is Nothing Then
MsgBox "Not Found"
Else
ActiveCell.Offset(0, 1).Value2 = RangeObj.Offset(0, 1).Value2
End If
ActiveCell.Offset(1, 0).Activate
Wend
End Sub
Let me know how it goes.
I have an Excel tracker that I put an "X" in a cell every month if a certain activity is accomplished.
This "X" correlates to a range of cells on the same sheet.
I want when I click on a command box;
If the cell for January has an "X" copy specific cells on the current page to specific cells on another work sheet.
If the cell for February has an "X" copy some other specific cells on the current page to some other specific cells on the other worksheet.
So on and so forth through December.
I have the following code (which does not work):
Private Sub CommandButton1_Click()
Sheets("MRT").Select
If InStr(1, (Range("L8").Value), "X") > 0 Then
Range("E42:AA42").Select
Selection.Copy
Sheets("Test '12").Select
Cells(3, AP).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone
End If
End Sub
Try this:
Private Sub CommandButton1_Click()
If Sheets("MRT").Range("L8").Value like "*X*" Then
Sheets("MRT").Range("E42:AA42").Copy
Sheets("Test '12").Cells(3, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone
End If
End Sub
Worked in my test, however you might want to adapt Cells(3,1) and the other position specifiers to your desired targets.
edit: forgot about the part with the months ... wait a minute ... here:
Sub FindSignificant()
Dim SearchString As String
Dim SearchRange As Range, cl As Range
Dim FirstFound As String
Dim sh As Worksheet
' Set Search value
SearchString = "a"
Application.FindFormat.Clear
' loop through all sheets
Set sh = Sheets("MRT")
' Find first instance on sheet
Set cl = sh.Cells.Find(What:=SearchString, _
After:=sh.Cells(1, 1), _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not cl Is Nothing Then
' if found, remember location
FirstFound = cl.Address
' format found cell
Do
Select Case sh.Cells(cl.Row, 1).Value
Case "december"
sh.Range("E42:AA42").Copy
Sheets("Test '12").Cells(3, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone
Case "february"
sh.Range("E42:AA42").Copy
Sheets("Test '12").Cells(3, 1).PasteSpecial Paste:=xlValues, Operation:=xlNone
Case Else
'do nothing
End Select
' find next instance
Set cl = sh.Cells.FindNext(After:=cl)
' repeat until back where we started
Loop Until FirstFound = cl.Address
End If
End Sub
this code origins from here
You would have to adapt the select case, but i really would think about solving this without VBA, if it is not necessary ;)