Aiuto selezione celle

akkarin

New Member
Joined
May 21, 2014
Messages
5
Ciao a tutti, sono nuovo del forum e anche se so l'inglese piuttosto bene preferisco chiedere in italiano.
Ho 26 anni e ho iniziato da poco a prendere in mano VBA per cercare di risolvere alcuni problemi contabili in azienda.

questo è il mio problema:

ho bisogno di selezionare tutte le celle che si trovano alla sinistra della cella attiva (ma sulla stessa riga, e cella attiva compresa). Supponendo che la cella attiva sia F21 ho bisogno di selezionare un Range che va da A21 a F21. La selezione deve andare bene per qualsiasi cella attiva del foglio di lavoro.

E' tutto il giorno che lavoro su questo progetto e sono talmente stanco che spero di aver scritto bene la richiesta.
 

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
Prova questo

Code:
Sub SelectToLeft()

Dim r As Range
Set r = ActiveCell.Cells
Range(r, r.End(xlToLeft)).Select


End Sub
 
Upvote 0
The code that Bruce posted will select from the active cell leftwards until you hit an empty cell. If that is what you need then great. However your post was a little unclear. Do you need that Bruce did? Or do you simply need to select from column A to the active cell? If so then:

translated by Google, so please forgive any errors / tradotta da Google, quindi ti prego di perdonare eventuali errori

Il codice che Bruce ha postato selezionerà dalla cella attiva verso sinistra fino a colpire una cella vuota. Se questo è quello che ti serve poi grande. Tuttavia il tuo post era un po 'poco chiaro. Avete bisogno che Bruce ha fatto? Oppure avete semplicemente bisogno di selezionare dalla colonna A alla cella attiva? Se è così allora:

[FONT=Consolas,]Sub SetFromAtoActive()

Dim rngAtoActive As Excel.Range

Set rngAtoActive = Range(Cells(ActiveCell.Row, 1), ActiveCell)

'// just checking.**delete this line once you're sure you're
'// getting the results you expect.
rngAtoActive.Interior.Color = vbYellow

End Sub
[/FONT]
 
Upvote 0
thank you for your answer. I tried the code that Bruce posted and it worked fine. I didn't have any empty cell so I didn't know that it wouldn't have worked.
I think I will going to use the code you just posted because I honestly don't know if I will find cases where there are empty cells between the active one and the cell in the A column. This is a project to convert invoices printed on file by an accounting program into something that can be sent by mail.

I am writing the VBA code using a small amount of invoices and it's hard to foresee all the possible cases.

Thank you both for your answers though :biggrin:

p.s.: i've found out that this code is working too:

Range("A" & (ActiveCell.Row)).Select
 
Last edited:
Upvote 0
seems like I can't edit again my last post. The last code i posted was just for another thing, nevermind. I'm a bit tired :LOL:
 
Upvote 0
Akkarin, your English is excellent!


Just a general programming note - you almost NEVER need to actually SELECT a range of cells. Yes, there are some rare sets of circumstances where you might actually have to SELECT a range. But easily 99% of the time you don't need to. Not relying on selecting cells makes your code much more robust and it makes it MUCH easier to debug. Just set a RANGE object variable to whatever group of cells you need to manipulate and do whatever needs to be done to the range object variable. (Again, my Italian is non-existent, so I'll let Google try to translate.)


(Ancora una volta, il mio italiano è inesistente, quindi mi lascerò Google tenta di tradurre.) Solo una nota programmazione generale - quasi mai necessario selezionare effettivamente un intervallo di celle. Sì, ci sono alcune rare serie di circostanze in cui si potrebbe effettivamente avere per selezionare un intervallo. Ma facilmente il 99% delle volte non è necessario. Non basandosi sulla selezione cellule rende il codice molto più robusto e rende molto più facile eseguire il debug. Basta impostare una variabile oggetto Range a qualsiasi gruppo di celle è necessario manipolare e fare ciò che deve essere fatto per la variabile oggetto Range.
 
Last edited:
Upvote 0
thank you for your answer. I tried the code that Bruce posted and it worked fine. I didn't have any empty cell so I didn't know that it wouldn't have worked.
I think I will going to use the code you just posted because I honestly don't know if I will find cases where there are empty cells between the active one and the cell in the A column. This is a project to convert invoices printed on file by an accounting program into something that can be sent by mail.

I am writing the VBA code using a small amount of invoices and it's hard to foresee all the possible cases.

Thank you both for your answers though :biggrin:

p.s.: i've found out that this code is working too:

Range("A" & (ActiveCell.Row)).Select


Ciao Akkarin,

I think what Greg meant to say was if there was a non empty cell. I wrote and tested my code on a blank worksheet. I went back and tried it again after Greg's post. This time I put some data between the cell I selected and the A column of the worksheet. The code only selects from the active cell to the cell with the data, it doesn't select cells all the A column if there is data in between. In your original post I think you said you wanted to go all the way to the A column, his code will work no matter what amount of data there is.

Penso che quello che Greg voleva dire era se ci fosse una cella non vuota. Ho scritto e provato il mio codice su un foglio di lavoro vuoto. Sono tornato e ho provato di nuovo dopo il post di Greg. Questa volta ho messo alcuni dati tra la cella Ho selezionato e Una colonna del foglio di lavoro. Il codice seleziona solo dalla cella attiva alla cella con i dati, non selezionare tutte le celle della colonna A se ci sono dati nel mezzo. Nel tuo post originale penso che hai detto che volevi andare fino in fondo alla colonna A, il suo codice funzionerà, non importa la quantità di dati che ci sia.
 
Last edited:
Upvote 0
well guys honestly I am a bit confused on what I need to do. And the problem is the same I was trying to explain to Greg in my last post: I don't actually know if there will be cases where I will not need to select all the cells all the way up to A column. I think I'll need to test this for a very long period before starting to use it as a secure method to send invoices by e-mail. The fun fact is that I am trying to write this code because I notice that we spend tons of money to buy postage stamps to send traditional mails.

Coming back into topic I want to say that I think there will not be empty cells in that special part of the invoice (it's the description of the goods, but our program use a strange method to separate fields, that's why I'm not sure).
 
Upvote 0
The code that Bruce posted will select from the active cell leftwards until you hit an empty cell...


...The code only selects from the active cell to the cell with the data...


OK, actually neither of these is 100% correct because the behavior of the END(xlToLeft) method is going to vary based on the contents of the starting cell and the contents of the cell immediately to the left of the starting cell. If the starting cell is empty then END will stop at the first non-empty cell. If the starting cell is not empty and the first cell to the left is empty, END is going to stop at the next non-empty cell. If cell immediately to the left is not empty, END is going to stop BEFORE the next empty cell.


Probably the easiest way to see what will happen is to demonstrate it. Note that in none of this test code is any cell or range SELECTed. If you want to step through the code like you were debugging using the F8 key in the VBE, you can go back to Excel, activate other workbooks, select cells in these other workbooks, etc. and the code will still run flawlessly.


OK, in realtà nessuno di questi è 100% corretto perché il comportamento del metodo END (xlToLeft) sta andando a variare in base al contenuto della cella di partenza e il contenuto della cella immediatamente a sinistra della cella iniziale. Se la cella di partenza è vuota allora END si ferma alla prima cella non vuota. Se la cella di partenza non è vuota e la prima cella a sinistra è vuota, END si fermerà al prossimo cella non vuota. Se la cella immediatamente a sinistra non è vuoto, END si fermerà prima del prossimo cella vuota.


Probabilmente il modo più semplice per vedere cosa accadrà è quello di dimostrarlo. Si noti che in nessuno di questo codice di test è un qualsiasi cella o dell'intervallo selezionato. Se si desidera eseguire il codice come se fossi il debug utilizzando il tasto F8 nel VBE, si può tornare a Excel, attivare altre cartelle di lavoro, selezionare le celle in queste altre cartelle di lavoro, ecc e il codice sarà ancora funzionare senza problemi.


Next post will show the code to demo this.
 
Upvote 0

Forum statistics

Threads
1,215,842
Messages
6,127,230
Members
449,371
Latest member
strawberrish

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