Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-18-2022, 05:58 PM
gbrew584 gbrew584 is offline Trying to get the shortest line of code for copying from one cell to the next Windows 7 64bit Trying to get the shortest line of code for copying from one cell to the next Office 2013
Novice
Trying to get the shortest line of code for copying from one cell to the next
 
Join Date: Apr 2015
Location: Ohio
Posts: 28
gbrew584 is on a distinguished road
Default Trying to get the shortest line of code for copying from one cell to the next

Hi, all and thanks for reading my question. I am not very good with vba but I am trying to learn. while I was practicing, I was wondering how short I could make a line of code.





So I made a word document that consist of 1 table. The table is 5 columns with 2 rows. In the fist cell on the first row I have the text "is this working".


I am trying to copy the text from the first cell on row 1 to the first cell on row 2.


I ran the code below. It captures the desired text but it doesn't leave (or insert, input) the text in the cell on row 2. (sorry if I am not using the correct terminology)



I think maybe the T1 and T2 variables are wrong but I am not sure what they should be other than String.


If someone could please let me know what I am missing I would appreciate it.



Sub CopyPaste1() ' copy information from 1 cell


Dim AD As Document
Dim TB As Tables
Dim ST As Integer
Dim C1 As Cell
Dim C2 As Cell
Dim RG1 As Range
Dim RG2 As Range
Dim T1 As String
Dim T2 As String

Set AD = ActiveDocument
Set TB = AD.Tables
ST = 1
Set C1 = TB(ST).Cell(1, 1)
Set C2 = TB(ST).Cell(2, 1)
Set RG1 = C1.Range
Set RG2 = C2.Range
T1 = RG1.Text
T2 = RG2.Text

T2 = Left(T1, Len(T1) - TB(ST).Rows.Count)

End Sub
Reply With Quote
  #2  
Old 03-18-2022, 06:04 PM
gbrew584 gbrew584 is offline Trying to get the shortest line of code for copying from one cell to the next Windows 7 64bit Trying to get the shortest line of code for copying from one cell to the next Office 2013
Novice
Trying to get the shortest line of code for copying from one cell to the next
 
Join Date: Apr 2015
Location: Ohio
Posts: 28
gbrew584 is on a distinguished road
Default

I should probably use trim instead of Len, too . It looks like trim will not work to remove the end of cell mark, i guess I'll have to stick with using Len for now.
Reply With Quote
  #3  
Old 03-18-2022, 06:49 PM
macropod's Avatar
macropod macropod is offline Trying to get the shortest line of code for copying from one cell to the next Windows 10 Trying to get the shortest line of code for copying from one cell to the next Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,375
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

As a single line of code:
Code:
With ActiveDocument.Tables(1): .Cell(2, 1).Range.Text = Split(.Cell(1, 1).Range.Text, vbCr)(0): End With
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #4  
Old 03-18-2022, 07:06 PM
gbrew584 gbrew584 is offline Trying to get the shortest line of code for copying from one cell to the next Windows 7 64bit Trying to get the shortest line of code for copying from one cell to the next Office 2013
Novice
Trying to get the shortest line of code for copying from one cell to the next
 
Join Date: Apr 2015
Location: Ohio
Posts: 28
gbrew584 is on a distinguished road
Default

Thanks very much for the reply, but that is not the result I was looking for (however, I do like the vbCr usage. I will have to look into that one a little deeper).


I am trying to end up with a line of code that would look something like this:
T2 = Left(T1, Len(T1) - 2)


Keeping all the Dim and Set statements I have. Its kinda an odd way to do it, but I'm just experimenting. Thanks for your help.
Reply With Quote
  #5  
Old 03-19-2022, 02:28 PM
macropod's Avatar
macropod macropod is offline Trying to get the shortest line of code for copying from one cell to the next Windows 10 Trying to get the shortest line of code for copying from one cell to the next Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,375
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

The conventional way of doing things would be something along the lines of:
Code:
Sub Demo()
Dim Rng As Range
With ActiveDocument.Tables(1)
  Set Rng = .Cell(1, 1).Range
  Rng.End = Rng.End - 1
  .Cell(2, 1).Range.Text = Rng.Text
End With
End Sub
What you need to understand is that the end-of-cell marker (i.e. ¤) consists of two characters but, like a paragraph break, occupies only one character space. Hence, moving the Range end backwards by one character is sufficient to eliminate the end-of-cell marker but, if you're using Len() and the like, you need to deduct two characters for the end-of-cell marker (e.g. Left(.Cell(1, 1).Range.Text, Len(.Cell(1, 1).Range.Text) - 2)).
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 03-19-2022, 09:28 PM
gbrew584 gbrew584 is offline Trying to get the shortest line of code for copying from one cell to the next Windows 7 64bit Trying to get the shortest line of code for copying from one cell to the next Office 2013
Novice
Trying to get the shortest line of code for copying from one cell to the next
 
Join Date: Apr 2015
Location: Ohio
Posts: 28
gbrew584 is on a distinguished road
Default

Thanks for the information and thanks for the suggestions I will give them a try.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying cell data from one tab to another tab tlmy87 Excel 0 12-23-2016 07:54 AM
Arrange shortest to longest sentence in Word omar Word 8 11-20-2015 04:38 PM
Trying to get the shortest line of code for copying from one cell to the next Copying a Changing cell value to one cell mbesspiata Excel 1 04-29-2014 02:16 PM
Copying data from one cell to another where condition is third cell fairyca Excel 4 03-30-2014 08:22 AM
Copying part of a cell to a new cell paulw793 Excel 7 01-27-2012 10:46 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 12:17 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft