Addition line of code for existing code

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Hi,
I am using the following code of which works well,

Code:
 Private Sub GrassSummaryIncomeSheet_Click()    Dim strFileName As String
    
        strFileName = "C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\INCOME 2019-2020\" & _
        Range("J3") & "_" & Format(Month(DateValue(Range("G3") & " 1, " & "2019")), "00") & " " & Range("G3") & ".pdf"


    If Dir(strFileName) <> vbNullString Then
        MsgBox "INCOME GRASS SHEET " & Range("G3") & " " & Range("J3") & " WAS NOT SAVED AS IT ALREADY EXISTS", vbCritical + vbOKOnly, "INCOME SUMMARY GRASS SHEET MESSAGE"
        Exit Sub
    End If
    
    With ActiveSheet
        .ExportAsFixedFormat Type:=xlTypePDF, Filename:=strFileName, Quality:=xlQualityStandard, IncludeDocProperties:=True
        MsgBox "INCOME GRASS SHEET " & Range("G3") & " " & Range("J3") & " WAS SAVED SUCCESSFULLY", vbInformation + vbOKOnly, "INCOME SUMMARY GRASS SHEET MESSAGE"
        Range("G5:H30").ClearContents
        Range("G5").Select
        ActiveWorkbook.Save
    End With


End Sub

The worksheet is called G INCOME & i would like to run the above code but also take note of the following,
Month in cell G3
Value in cell J31
Value in cell K31

With the above info we now need to go to another worksheet so,

The path is C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\SUMMARY SHEET\SHEET
Months column range C5:C17
Income column range D5:D17
Mileage column range E5:E17

So then as follows,
WORKSHEET G INCOME cell G3 to WORKSHEET SHEET look in the range of C5:C17 for a match then

WORKSHEET G INCOME cell J31 to WORKSHEET SHEET cell in the range of D5:D17

WORKSHEET G INCOME cell K31 to WORKSHEET SHEET cell in the range of E5:E17

The code would need to somehow pick out the matching month in range C5:C17 then paste the two values into its corresponding row cell

Here is a photo to give you some visual info.
As you will see there are 2 months of APRIL

5536.jpg
 
Last edited:

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Would this be the FIND method to use in this case ?

I operate the macro button.
The code is told to look at the value in cell G3 on its worksheet called G INCOME "this example July"
It then collects the value from cells J31 "this example £54.99" & K31 "this example 5"

Then these two collected values " £54.99 & 5" needs to be saved to worksheet C:\Users\Ian\Desktop\GRASS CUTTING\CURRENT GRASS SHEETS\SUMMARY SHEET\SHEET
So,

Search ONLY in column range C5:C17 for a match taken from worksheet G INCOME cell G3 "this example JULY"

Once the code finds the match "JULY" then paste data collected from cells J31 " this example £54.99" & K31 "5" into the cells to the right.

So G INCOME CELL J31 "this example £54.99" to SHEET next to its found match "so in this case JULY is in cell C8 so paste "£54.99" in FIRST cell to the right so this would be cell D8

THEN

So G INCOME CELL K31 "this example 5" to SHEET next to its found match "so in this case JULY is in cell C8 so paste "5" in SECOND cell to the right so this would be cell E8

Then save SHEET.

Finished,so SHEET Now has a value next to JULY in cell D8 of £54.99 & also cell E8 of 5

When i run this again next month it will look in the given cell of which will be worksheet G INCOME month AUGUST,find August in the cell range on the worksheet SHEET then paste the £ value in cell D9 & also the numerice number into cell E9

Many thanks & have a nice day
 
Upvote 0
Morning,
I have made some progress last night but would like to have some help for the position of where the values are saved.

My problem is that the values do not get put in the cells to the right of the month.
On the destination sheet of which the values are saved the month is located in column C
So J31 value should be placed in cell to the right of the month thus D9
Then K31 value should be placed in the next right hand cell thus E9

Here is the code in use.

Code:
Option ExplicitPrivate Sub TransferIncomeInfo_Click()
    Dim rFndCell As Range
    Dim strData As String
    Dim stFnd As String
    Dim fCol As Integer
    Dim sh As Worksheet
    Dim ws As Worksheet
    
    Set ws = Sheets("G INCOME")
    Set sh = Sheets("G SUMMARY")
    stFnd = ws.Range("G3").Value
     
        With sh
            Set rFndCell = .Range("C5:C17").Find(stFnd, LookIn:=xlValues)
                If Not rFndCell Is Nothing Then
                    fCol = rFndCell.Column
                    ws.Range("J31,K31").Copy sh.Cells(6, fCol)
                    MsgBox "Transfer Has Been Completed", vbInformation + vbOKOnly, "INCOME TRANSFER SHEET MESSAGE"
                Else
                    MsgBox "DOES NOT EXIST"
            End If
        End With
         
    End Sub

The destination sheet BEFORE transfer has run.

EXCEL%20BEFORE.jpg


The destination sheet AFTER transfer has run.

EXCEL%20AFTER.jpg


The month used in the example was JULY

As you can see that the values have overwritten MAY & are placed in column C & D as opposed to being placed to the right of JULY in columns D & E

So ive managed to get the values from G INCOME sheet to the G SUMMARY sheet but need some help please with the correct placement of the values.

Have a nice day.

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top